How to write lookup method for reference field in Dynamics 365 for Operations.
Problem statement
Created customized Enum field in employee master in HR module. Enums are Active employees, Inactive employees & Resigned employees.
Requirement is only active employee should filter in “Owner” field in Department master in HR module
Detailed step by step resolution for Lookup Method for Reference Field in Dynamics 365
- Copy event handler from HCM worker form
- Create new class paste the event handler
- Apply the below code
- 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();
}
Dynamics 365 for Operations Technical Consultant | Passionate about coding, customizations, and workflow optimization