Lookup method for reference field in Dynamics 365 for Operations

How to write a Lookup method for reference field in Dynamics 365 Finance and Operations.

Problem statement

Created a customized Enum field in the employee master in the HR module. Enums are Active employees, Inactive employees & Resigned employees.

The requirement is that only active employees should filter in the “Owner” field in the Department master in the HR module

Detailed resolution steps

  • Copy the event handler from the HCM worker form 
  • Create a new class, paste the event handler
  • Apply the code below
  • Compile 

Microsoft Dynamics AX 2012 introduced a new form control type: the reference group. And also, very often we’ll have to customize the contents of the lookup, or filter them. We can do this by using the SysReferenceTableLookup class.

So go to your form’s data source and find the reference field for which you want to perform the lookup. Override its lookupReference method.

The use of the SysReferenceTableLookup is very similar to the SysTableLookup: we construct a Query, add data sources and ranges to it, and then pass it to the SysReferenceTableLookup object, like the code below:

Code snippet

public static void  activeEmployeesLookup(FormControl sender, FormControlEventArgs e)

{

     SysReferenceTableLookup sysReferenceTableLookup = SysReferenceTableLookup::newParameters(tableNum(HcmWorker),   sender);

     Query query = new Query();

     QueryBuildDataSource qbdsHcmWorker, qbdsHcmEmployment;

     qbdsHcmWorker = query.addDataSource(tableNum(HcmWorker));

     qbdsHcmEmployment = qbdsHcmWorker.addDataSource(tableNum(HcmEmployment));

     qbdsHcmEmployment.joinMode(JoinMode::InnerJoin);

     qbdsHcmEmployment.relations(true);

     qbdsHcmEmployment.addRange(fieldNum(HcmEmployment, ISLEmploymentStatus)).value(queryValue(ISLEmploymentStatus::Active));

     sysReferenceTableLookup.parmQuery(query);

     sysReferenceTableLookup.addLookupfield(fieldNum(HcmWorker,   PersonnelNumber));

     sysReferenceTableLookup.addLookupfield(fieldNum(HcmWorker,   Person));

     sysReferenceTableLookup.performFormLookup();

     FormControlCancelableSuperEventArgs cancelableSuperEventArgs = e as FormControlCancelableSuperEventArgs;

     cancelableSuperEventArgs.CancelSuperCall();

}