Use Event Handlers to Override a Form Control Lookup Method in Dynamics 365 Finance and Operations
Utilizing event handlers to override a Form Control Lookup Method in Dynamics 365 Finance and Operations gives developers the ability to present users with a specific variety of columns from a related table.
For example, Vendor group is an attribute on the Vendor record (VendTable form), which pertains to a list of potential records in the VendGroup table. In the case of the lookup method on the vendor record, two columns are presented to users, vendor group, and description:
Choosing a vendor group from the list will associate this vendor with that vendor group.
What if your users want to see more than simply the group ID and description? In my scenario, I am asked to also present the payment terms associated with that vendor group. I can use an event handler to accomplish this.
First, we need to find the control in question within Visual Studio and find its events. It's beneficial to right-click on the control in question within Dynamics and navigate the context menu to Form information. There, you can find the control name, in this case, Posting_VendGroup:
Within Visual Studio, create a new project and navigate the Application Explorer to AOT > User Interface > Forms > VendTable, right click and choose Open designer:
In the designer window, search for the control we identified earlier (Posting_VendGroup), which highlights it on the right-hand design tree:
Expand the control, expand the Events node, and find the OnLookup event:
Right-click the OnLookup event, and choose Copy event handler method:
Create a new class in your project by right-clicking your project and selecting Add. Name this MyVendTableForm_Handler:
The new class MyVendTableForm_Handler will open. Now, let's paste the event handler text we copied earlier into this class.
In my case, I will insert a new line after line 2, and paste it:
Now that we have the event handler signature in our new class, we will now override the lookup columns with our desired addition of the payment terms. The completed code is listed below:
internal final class MyVendTableForm_Handler { /// <summary> /// Event handler to replace the existing VendGroup lookup with customized version (addition of payment terms) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> [FormControlEventHandler(formControlStr(VendTable, Posting_VendGroup), FormControlEventType::Lookup)] public static void Posting_VendGroup_OnLookup(FormControl sender, FormControlEventArgs e) { SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(VendGroup), sender); // VendGroup columns presented to user sysTableLookup.addLookupfield(fieldNum(VendGroup, VendGroup)); sysTableLookup.addLookupfield(fieldNum(VendGroup, Name)); sysTableLookup.addLookupfield(fieldNum(VendGroup, PaymTermId)); // Execute lookup sysTableLookup.performFormLookup(); // Use FormControlCancelableSuperEventArgs to cancel call to super() // Prevents the system from attempting to show the lookup form twice, causing error. FormControlCancelableSuperEventArgs cancelableSuperEventArgs = e as FormControlCancelableSuperEventArgs; cancelableSuperEventArgs.CancelSuperCall(); } }
Finally, save and build your project. When complete, navigate back to the VendTable form and click the vendor group control. We can now see our new Terms of payment column has been added successfully:
Mission accomplished.
One thing to note, for those that may have noticed the CancelSuperCall at the bottom of our code:
FormControlCancelableSuperEventArgs cancelableSuperEventArgs = e as FormControlCancelableSuperEventArgs; cancelableSuperEventArgs.CancelSuperCall();
This is important because it eliminates multiple lookup forms from occurring during this process. Users will encounter this error when they click on the vendor group control:
Questions?
Please reach out to us! Stoneridge Software has a team of dedicated experts happy to assist you.
Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.
Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.