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

Thu Dec 29 2011

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
        // 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));

        // 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));

        // anything else, quit

    // set the query

    // perform the lookup

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

public void performFormLookup(FormRun _form, FormControl _formControl)
    // show the order lookup