AX 2012: Create lookup in X++

To create a lookup on a control (e.g. StringEdit control), override the lookup method of that control and copy paste the following code snippet in it. Of course, you will need to substitute datasource of the query according to your requirements.

public void lookup()
    Query query;
    QueryBuildDataSource datasourceModule;
    QueryBuildDataSource datasourceLanguage;
    QueryBuildRange rangeElementType;
    QueryBuildRange rangeModuleId;
    SysTableLookup sysTableLookup;
    query = new Query();
    datasourceLanguage = query.addDataSource(tableNum(SysModelElement));
    datasourceModule = datasourceLanguage.addDataSource(tableNum(SysModelElement));
        fieldNum(SysModelElement, ParentModelElement),
        fieldNum(SysModelElement, RecId));

        fieldNum(SysModelElement, ElementType)).value(int2str(UtilElementType::LabelFile));

    datasourceModule.addRange(fieldNum(SysModelElement, Name)).value(ModuleId.text());
    sysTableLookup = SysTableLookup::newParameters(tableNum(SysModelElement), this);
    sysTableLookup.addLookupfield(fieldNum(SysModelElement, Name));

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s