Custom lookups are a recurring topic in Axapta discussions and blog posts. The lookup functionality in Ax is versatile, flexible and has changed over the different versions.
This post is about programming your lookup without the need of defining a separate lookup form, as this functionality isn't that well documented in Ax. We'll do this by using class SysTableLookup.
Let's look at a full code example first.
public void lookup()
{
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(CustTable),this,true);
Query query;
QueryBuildDataSource queryBuildDataSource;
;
sysTableLookup.addLookupfield(fieldnum(CustTable,AccountNum),true);
sysTableLookup.addLookupfield(fieldnum(CustTable,Name));
query = new Query();
queryBuildDataSource = query.addDataSource(tablenum(CustTable));
queryBuildDataSource.addSortField(fieldnum(CustTable,AccountNum));
queryBuildDataSource.addRange(fieldnum(CustTable,Currency)).value(queryvalue('EUR'));
sysTableLookup.parmQuery(query);
sysTableLookup.parmUseLookupValue(true);
sysTableLookup.performFormLookup();
}
You can use the code above to override the lookup method of a control in a form.
Now let's have a look at the different parts in the code.
First, we create the table lookup by instantiating the sysTableLookup class.
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(CustTable),this,true);
The form control from which the code is called is included (this).
We define which fields to include in our lookup form.
The second parameter is set to true, if that's the field you wish to return from your lookup.
If you accidentally set it multiple times to true, the field where it's last set will be the lookup field, so your lookup may not work correctly.
sysTableLookup.addLookupfield(fieldnum(CustTable,AccountNum),true);
sysTableLookup.addLookupfield(fieldnum(CustTable,Name));
Now we create the actual query, setting sorting fields and ranges as desired.
query = new Query();
queryBuildDataSource = query.addDataSource(tablenum(CustTable));
queryBuildDataSource.addSortField(fieldnum(CustTable,AccountNum));
queryBuildDataSource.addRange(fieldnum(CustTable,Currency)).value(queryvalue('EUR'));
We pass along the parameters to our sysTableLookup...
sysTableLookup.parmQuery(query);
sysTableLookup.parmUseLookupValue(true);
And we're ready to go
sysTableLookup.performFormLookup();
I tend to use this functionality, when using edit methods instead of display methods in a form (where the return EDT is for example Name, and not a specific extended data type).
Showing posts with label SysTableLookup. Show all posts
Showing posts with label SysTableLookup. Show all posts
Monday, April 19, 2010
Subscribe to:
Posts (Atom)