AX 2012: Create lookup in X++

Posted on Updated on

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));
    datasourceModule.joinMode(JoinMode::InnerJoin);
    datasourceModule.relations(false);
    datasourceModule.addLink(
        fieldNum(SysModelElement, ParentModelElement),
        fieldNum(SysModelElement, RecId));

    datasourceModule.addRange(
        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));
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
}
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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