Use Event Handlers to Override a Form Control Lookup Method in Dynamics 365 Finance and Operations

By Andy Lawton-Thesing | November 3, 2022

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:

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations Vendor Record

Choosing a vendor group from the list will associate this vendor with that vendor group.

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations 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:

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations Form Information

Within Visual Studio, create a new project and navigate the Application Explorer to AOT > User Interface > Forms > VendTable, right click and choose Open designer:

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations Visual Studio

In the designer window, search for the control we identified earlier (Posting_VendGroup), which highlights it on the right-hand design tree:

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations Designer Window

Expand the control, expand the Events node, and find the OnLookup event:

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations OnLookup

Right-click the OnLookup event, and choose Copy event handler method:

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations Copy Event Handler Method

Create a new class in your project by right-clicking your project and selecting Add. Name this MyVendTableForm_Handler:

Picture8

The new class MyVendTableForm_Handler will open. Now, let's paste the event handler text we copied earlier into this class.

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations VendTable

In my case, I will insert a new line after line 2, and paste it:

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations VendTable Form

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:

Picture11

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:

Override a Form Control Lookup Method in Dynamics 365 Finance and Operations 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.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!