How to create a lookup in Dynamics AX that is conditional, based on a different field

Posted on December 29, 2011 by Michael Roma

In the example below, the lookup should be different based on the DRType field.

First create a function on the table where the field you want to have a lookup for, this case the table is called InventDR:

void orderIdLookup(FormControl _formControl)
{
    // function that shows the look for the dr type

    // define lookup form, and query
    SysTableLookup       lookupForm;
    Query                query = new query();
    QueryBuildDatasource qbds;
    ;

    // setup the lookup form and query
    switch(this.DRType)
    {
        // production
        case DRTypeBase::Production:

            // setup the form and query
            lookupForm = SysTableLookup::newParameters(tablenum(ProdTable), _formControl);
            qbds = query.addDataSource(tablenum(ProdTable));

            // add the lookup fields
            lookupForm.addLookupfield(fieldnum(ProdTable, ProdId));
            lookupForm.addLookupfield(fieldnum(ProdTable, Name));
            break;

        // purchase order
        case DRTypeBase::PurchaseOrder:

            // setup the form and query
            lookupForm = SysTableLookup::newParameters(tablenum(PurchTable), _formControl);
            qbds = query.addDataSource(tablenum(PurchTable));

            // add the lookup fields
            lookupForm.addLookupfield(fieldnum(PurchTable, PurchId));
            lookupForm.addLookupfield(fieldnum(PurchTable, PurchName));
            break;

        // anything else, quit
        default:
            return;
    }

    // set the query
    lookupForm.parmQuery(query);

    // perform the lookup
    lookupForm.performFormLookup();
}

Next, override the performFormLookup() function on the Form Datasource Field:

public void performFormLookup(FormRun _form, FormControl _formControl)
{;
    // show the order lookup
    InventDR.orderIdLookup(_formControl);
}
comments powered by Disqus