Monday, July 14, 2014

Multi-Table Lookups (SysMultiTableLookup)

When creating an override lookup method that needs to return data from multiple tables you will need to be sure to use the SysMultiTableLookup class instead of SysTableLookup class. If you do not then whenever you run the fieldnum() function it will try to use the field id on the initial table instead of the one you are referring to. Below is an example on how to run a lookup to get the UOM symbols and descriptions for a special unit class uom.

 Query                   query;
    QueryBuildDataSource    queryBuildDataSourceTable;
    QueryBuildDataSource    queryBuildDataSourceTableDescriptions;
    QueryBuildRange         queryBuildRange;
    //you need to define the multitable, but initalize it AFTER you have defined the query
    //SysTableLookup          sysTableLookup  = SysTableLookup::newParameters(tableNum(UnitOfMeasure), ctrl);
    SysMultiTableLookup sysTableLookup;
    query = new Query();

    queryBuildDataSourceTable = query.addDataSource(tableNum(UnitOfMeasure));
    //join the translation table so we can get a description of the UOM
    queryBuildDataSourceTableDescriptions = queryBuildDataSourceTable.addDataSource(tableNum(UnitOfMeasureTranslation));
    queryBuildDataSourceTableDescriptions.joinMode(JoinMode::InnerJoin);
    queryBuildDataSourceTableDescriptions.relations(false);
    queryBuildDataSourceTableDescriptions.addLink(fieldNum(UnitOfMeasure,RecId),fieldNum(UnitOfMeasureTranslation,UnitOfMeasure));

    //filter by the unit class being passed
    queryBuildRange = queryBuildDataSourceTable.addRange(fieldNum(UnitOfMeasure, UnitOfMeasureClass));
    queryBuildRange.value(queryValue(unitClass));
    //define multiple table lookup query
    sysTableLookup  = SysMultiTableLookup::newParameters(ctrl, query);
    //add which fields will be displayed to the user (symbol + desc.)
    sysTableLookup.addLookupfield(fieldNum(UnitOfMeasure, Symbol), true);
    sysTableLookup.addLookupfield(fieldNum(UnitOfMeasureTranslation, Description), 2);
    //do not use for multi table
    //sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();

5 comments:

  1. Hi,

    I am using the above mentioned piece of code in AX 2012 but the system is throwing an Error saying, SysMultiTableLookup variable has not been declared.

    Is it specific for AX 2009?

    ReplyDelete
  2. Did you replace the ctrl variable with the control you are trying to hijack?

    ReplyDelete
  3. Replies
    1. Amber, check out this thread https://community.dynamics.com/365/financeandoperations/f/dynamics-365-for-finance-and-operations-forum/316352/multitable-lookup


      It should give you some ideas on how to tackle this.

      Delete