Search code examples
axaptax++dynamics-ax-2012

Get customer or prospects in Dynamics AX 2012


I am trying to get customer or prospect using X++ and use it on a lookup. There is a method in the DirPartyTable that returns what I want.

DirPartyTable::isCustomerOrRelation

while select * from dirPartyTable
{
       if(DirPartyTable::isCustomerOrRelation(dirPartyTable.RecId))
       {

                //Get the Name
                //info(dirPartyTable.Name);
       }
}

But when I build a query to the lookup I am trying to pass the DirPartyTable::isCustomerOrRelation(dirPartyTable.RecId) on the addRange of the query somehow. Is there a way to do it or is it impossible ?


Solution

  • If you go to the source of isCustomerOrRelation (and isCustomer and isRelation) you see, that the method returns true if a customer or prospect exists in the current company.

    Your while select, while correct, is inefficient, because it may have to scan a million parties to select on thousand customers or prospects present in you current company.

    A more efficient, but syntactical illegal, while select would be:

    while select * from dirPartyTable
       exists join custTable
       where custTable.Party == dirPartyTable.RecId
       union 
       select * from dirPartyTable
       exists join smmBusRelTable
       where smmBusRelTable.Party == dirPartyTable.RecId;
    {
         info(dirPartyTable.Name);
    }
    

    While illegal in X++ it is possible using queries and views.

    1. Make two queries (translate to appropriate properties yourself):

    2. Query1:

      select * from dirPartyTable
          exists join custTable
          where custTable.Party == dirPartyTable.RecId
      
    3. Query2:

      select * from dirPartyTable
          exists join smmBusRelTable
          where smmBusRelTable.Party == dirPartyTable.RecId;
      
    4. Make two views (View1 and View2) based on the queries.

    5. Make a union query (Query3), see how to Combine Data Sources in a Union Query, remember to specify the UnionType (Union or UnianAll).

    6. Make a view based on the Query3, see how to Create a View Based on a Query.

    Result, select all records using X++:

    while select * from dirPartyCustOrRelationTable
    {
         info(dirPartyCustOrRelationTable.Name);
    }
    

    Or you can use the Query3 directly to retrieve the records.