Home / Questions / lookup() - Form data source field methods in D365 F&O - X++ Code
Explanatory Question

lookup() - Form data source field methods in D365 F&O - X++ Code

👁 150 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

The lookup() method is a standard method on the field of the form data source. This method is called when a lookup operation is attempted on a data source field. If data that's shown in the lookup form must be customized with some filter, the lookup() method should be overridden.

Example

You have a custom form with a table named TrainingMaster. The table has a field named TrainerID that creates a foreign key with the TrainerTableTrainerTable has an enum field called TrainerType that has two values: Technical and Functional. The lookup in the TrainerID field should display only technical trainers.

The coding pattern can be as follows:


[DataSource]
class TrainingMaster
    {
        [DataField]
        class TrainerID 
        {
            public void lookup(FormControl _formControl, str _filterStr)
            {
                SysTableLookup sysTableLookup = 
                   SysTableLookup::newParameters(tableNum(TrainerTable),_formControl);
                
                Query query = new Query();
                QueryBuildDataSource qbds;
                qbds= query.addDataSource(tableNum(TrainerTable));
                qbds.addRange(fieldNum(TrainerTable, 
                     TrainerType)).value(enum2Str(TrainerType::Technical));

                sysTableLookup.addLookupfield(fieldNum(TrainerTable,TrainerID));
                sysTableLookup.addLookupfield(fieldNum(TrainerTable,TrainerName));

                sysTableLookup.parmQuery(query);
                sysTableLookup.performFormLookup();
            }
        }
    }