Sunday, July 8, 2018

How to Create a lookup without Data Source In AX


This post is dedicated to everyone who already made the question “How the hell do I insert a combo box here?” This happens a lot when you want to use a combo box as a filter.
It should be something simple, maybe it is and I haven’t found yet, but many of us have tried to drag and drop a table to data source and then using the lookup field or using a combo box control.
None of it will work and the only solution I have found so far is to create a lookup without data source.
To illustrate my example I have this form:
ScreenClip
As you can see, I would like to use ItemId as a filter but I have no data source.
1. On the form Design, I have created a new StringEdit Control and renamed it to StringEdit_ItemId. See form structure below.
ScreenClip
2. Expand the StringEdit_ItemId control and right click on Methods > Override Method > lookup. Like the image below:
ScreenClip
3. Now, insert the following code and we are done!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Override the method lookup()
public void lookup()
{
    Query                   query;
    QueryBuildDataSource    qbds;
    SysTableLookup          lookup;
    ;
 
    // Create the query for the lookup
    query   = new query();
    qbds    = query.addDataSource( tableNum(InventTable));
 
    // Instantiate sysTableLookup object using table which will provide the visible fields
    lookup  = SysTableLookup::newParameters( tableNum(InventTable), this);
 
    // Add fields that will be shown in the lookup as columns
    lookup.addLookupfield( fieldNum(InventTable,ItemId));
    lookup.addLookupMethod( tableMethodStr(InventTable,itemGroupId));
    lookup.addLookupMethod( tableMethodStr(InventTable,defaultProductName));
    lookup.addLookupfield( fieldNum(InventTable,NameAlias));
    lookup.addLookupfield( fieldNum(InventTable,ItemType));
    lookup.addSelectionField( fieldNum(InventTable,Product));
 
    // Add the query to the lookup form
    lookup.parmQuery(query);
 
    // Perform the lookup
    lookup.performFormLookup();
}

No comments:

Post a Comment