Simple field lookup in form
This is a simple way to add a custom form lookup to a new field with x++ code. This example is done with a test table named FO_TestTable that has the three fields “TestField1″, “TestField2″ and “TestField3″. The table FO_TestTable has an index named “TestIdx”.
I have added a field on the table CustTable named TestField1. On this field that now can be found on CustTable form on the CustTable datasource node, I have added the “Override method” “lookup”.
Below you can see how I have edited the method lookup to fit my needs. Notice that the sysTableLookup is based on an ordinary query, this means that you can manipulate this query as you see fit.
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 30 31 32 33 34 35 36 37 38 39 40 | public void lookup(FormControl _formControl, str _filterStr) { SysTableLookup sysTableLookup = SysTableLookup::newParameters( tablenum(FO_TestTable), _formControl); Query query; QueryBuildDataSource queryBuildDataSource; ; // Parameter "true" indicates what field // is returned to the form sysTableLookup.addLookupfield( fieldnum(FO_TestTable, TestField1), true); sysTableLookup.addLookupfield( fieldnum(FO_TestTable, TestField2)); sysTableLookup.addLookupfield( fieldnum(FO_TestTable, TestField3)); query = new Query(); // Add datasource to the query queryBuildDataSource = query.addDataSource( tablenum(FO_TestTable)); // Add sorting index to query queryBuildDataSource.addSortIndex( indexnum(FO_TestTable, TestIdx)); sysTableLookup.parmQuery(query); sysTableLookup.performFormLookup(); } |
There are easier ways to do this of course, one way would be by utilizing the AutoLookup field group on the FO_TestTable table. But that means that the form lookup will always look the same no matter what form the field is located in, and that might not always be enough depending on how you want to sort FO_TestTable in the different lookups. This field is located in the CustTable form, if it where also located in the VendTable form we might not want to show the same data in that lookup.
Tags: addDataSource, addLookupfield, FormControl, lookup, parmQuery, performFormLookup, query, SysTableLookup
November 7th, 2008 at 16:39
I have a table with a field called “fieldName.” It needs to be populated with a field name from another table. I can’t figure out how to make my form field lookup show all the field names from another table. I saw that “formName” shows all the forms in the AOT. There’s an extended data type called “fieldName,” but I don’t know what that’s for or if I can use it. If I can, how do I tell it which table I want the fieldNames from? I tried to write a custom lookup as well, but I can’t remember how to load data into a drop-down without making it a lookup on values from a table. Any ideas?
March 5th, 2010 at 19:58
I’m trying to customize a lookup field in AX 09 to not allow users to add data in the lookup data field and override what’s in the drop down menu.
Is that what the override method does? I want to disable any users from adding content that is not in the lookup table. Any thoughts?
March 8th, 2010 at 10:31
I don´t think you can/should use this method for validations of the content put in by the user. Use the override “validate” method instead and check if the value is correct, if not just return false and present a warning to the user of the reason the validation failed.
April 5th, 2010 at 17:05
what is a code sample of returning a validation failed? if the value is not what they selected in the lookup.
April 5th, 2010 at 17:09
Currently my code looks like this in the form, I need to add validation to check the lookup value, and only select data from the lookup.
public void lookup(FormStringControl _formControl, str _filterStr)
{
Args args;
FormRun formRun;
;
args = new Args(formstr(CIT_StateIDlookup));
args.caller(this);
formRun = ClassFactory::formRunClassOnClient(args);
formRun.init();
_formControl.performFormLookup(formRun);
}
April 6th, 2010 at 09:45
But in genereal you use each override method as they are meant to be used. The lookup method is used to filter a lookup and return a specific value. The modified method is used to do different things triggered by a specific fields change. The validate method is used to validate the value that the lookup has returned or that has been entered by the user. This is a good example of how validate methods work:
http://www.axaptapedia.com/Validate_field_values_on_form
April 6th, 2010 at 14:39
thank you, I appreciate your help. After setting up the method validate to the form. It still let’s you override the data and type in anything in the form field, instead of just selecting from the lookup menu.
April 6th, 2010 at 15:11
Example: Let´s say you create a new table and one of the fields is there to store ItemId. If you use the standard EDT for ItemId = ItemId, then you cannot enter an ItemId that does not exists in the InventTable. This is because the relationship automaticly validates the input in the field.
Also found this which might be of assistance to you:
http://axapta.blogcu.com/lookups/3357200