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.

Last 5 posts in Development

8 thoughts on “Simple field lookup in form

  1. 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?

  2. 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?

  3. The override method gives you the option to control what the lookup will present to the user and what value should be returned. Maybe you just want a field of the type ItemId to just present the items that is of the type service or BOM.

    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.

  4. 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);
    }

  5. Maybe we aren´t talking about the same thing here, I see you are using an external form as a lookup which my examle doesn´t.

    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

  6. 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.

  7. Ok, I have another solution that might help you. Try to setup a relation on the EDT you are using for the field on the table.

    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

Leave a Reply to bruce Cancel reply

Your email address will not be published. Required fields are marked *