Monday, April 26, 2010

Basic Survival Kit - Article #1 : Customizing Lookups by code

Here we are again :)

Sometimes I posted complex and crazy articles trying to be original and showing strange behaviours or new concepts(at least for me) , but ... there exists a wide range of daily problems which every Dynamics AX developer needs to solve frequently and sometimes is not easy to find useful information.

With this article I will start a serie of articles dedicated to show some necessary basic concepts in the daily life of a Dynamics AX developer.

Please note that almost all of the articles of this serie will be intended to help those Dynamics AX beginner developers that are working hard to learn how to customize this wonderful ERP.


Customizing Lookups by code

There are some situations on which we must to perform a custom lookup operation to show to the user some filtered records or for any other special reason.
There exists some classes designed to help us to perform this tasks.

Let's see a simple example, below is the code we wrote inside form control's lookup method  :




public void lookup()
{
    // SysTableLookup is the base class to perform many lookups
    SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tablenum(InventTable),this);
   
    // We already know how to create a query :)
    Query                   query = new Query();
    QueryBuildDataSource    queryBuildDataSource = query.addDataSource(tablenum(InventTable));
    ;
    // On lookup we don't call super()
    //super();

    // What fields we want into lookup grid :)
    sysTableLookup.addLookupfield(fieldnum(InventTable,ItemId));
    sysTableLookup.addLookupfield(fieldnum(InventTable,ItemName));
    sysTableLookup.addLookupfield(fieldnum(InventTable,ItemGroupId));
    sysTableLookup.addLookupfield(fieldnum(InventTable,NameAlias));
    sysTableLookup.addLookupfield(fieldnum(InventTable,ItemType));

    // Here is our customizations for this example : we only want items which belongs to ItemGroup 02
    queryBuildDataSource.addRange(fieldnum(InventTable,ItemGroupId)).value('02');
   
    // Here we're passing the query to systablelookup as a parameter
    sysTableLookup.parmQuery(query);
   
    // And here, finally, he launch the lookup
    sysTableLookup.performFormLookup();

   
}


As we can see on the code above, we are using SysTableLookup class to prepare and finally perform the lookup we want.
We just prepared a Query with all of the filters we need and after that we can perform the lookup.





There are another way to perform a custom lookup : Creating a custom lookup form, you can find a nice example in the Axaptapedia (here)


Greetz,

Mkz.

1 comment:

  1. What about this implementation?

    http://axwonders.blogspot.com/2011/03/filter-activity-form-contact-to-only.html

    ReplyDelete