How To Override An Existing Lookup Method: Chain Of Command

Share this:

In this article you will learn how to override an existing lookup method that exists on a form that is in a different model than your own. In a previous article we looked at how to override the lookup method in D365. However, there are some extra steps needed to learn how to override an existing lookup method in D365.

What Are Lookup Methods?

If you are new to lookup methods, please start with this article: How To Create a Lookup Method In D365 Forms.

It explains that lookup methods allow users to view multiple columns from a related record to help with selecting a value for the current record. This forms the basis for any relational database. First, it really speeds up data entry for the user. And secondly it helps ensure data integrity is maintained.

When designing a table in D365, a developer can specify the default fields to be shown in a lookup. However, a developer can also override the columns shown in the lookup for use on a particular form. On top of that, developers can also filter the rows returned in a lookup to be a subset of all the records in the table. This is really powerful and again makes the form even easier for a user to use.

In a previous article, I showed you how to override the lookup method. However, this was on a form that you created as part of your model. There are times when you will want to override the lookup method on an existing form that is not part of your model. This requires some additional steps.

Two Ways To Override An Existing Lookup Method

There are actually two ways to override an existing lookup method.

I will explain both methods. No pun intended. Ultimately, I do not know that one way is preferred over the other. I am more partial to using Chain Of Command, because I find the attribute syntax easier to write. And I tend to put all of my other extension code in a Chain Of Command class. But please use whatever you are most comfortable with.

If you are new to Chain Of Command, I recommend you read this previous article on Chain Of Command Fundamentals.

The Example Scenario

In this section, consider the example that you want to override an existing lookup method on the customer details form. Go to customer form by searching for ‘all customers’ in the search bar. Then select a customer to open the customer details form.

The CustTable form, contains a field that lets a user select the customer group. By default this lookup method shows two columns. The customer group ID and the Description.

Let’s pretend we wish to show a third column. The related Customer Group form contains a field named ‘Terms of payment’. In this example, I will show you how to display additional columns in the lookup.

Override An Existing Lookup Using An Event Handler

In this previous article I showed you how you can use a form event handler to override an existing lookup. The key in this situation, is to use CancelSuperCall.

class TutorialCustTable_Form_Handler
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(CustTable, Posting_CustGroup), FormControlEventType::Lookup)]
    public static void Posting_CustGroup_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        SysTableLookup      sysTableLookup  = SysTableLookup::newParameters(tableNum(CustGroup), sender);
        
        // Add the lookup columns
        sysTableLookup.addLookupfield(fieldNum(CustGroup, CustGroup));
        sysTableLookup.addLookupfield(fieldNum(CustGroup, Name));
        sysTableLookup.addLookupfield(fieldNum(CustGroup, PaymTermId));

        // Run the lookup
        sysTableLookup.performFormLookup();

        //cancel the call to super() to prevent the system from trying to show 
        //the lookup form twice and cause an error.
        FormControlCancelableSuperEventArgs cancelableSuperEventArgs = e as FormControlCancelableSuperEventArgs;
        cancelableSuperEventArgs.CancelSuperCall();
    }
}

Override An Existing Lookup Using Chain Of Command

Secondly, we will look at how to override an existing lookup using Chain Of Command. Create a new class, by right clicking on the project and select Add>Class. Name the class TutorialCustTable_Form_Extension.

Now that the class is created, we need to add the attribute to above the class definition to indicate to the compiler that code in this class extends the code in the form CustTable. See this article for how to use chain of command on form methods. Your code should look like this to start.

[ExtensionOf(formStr(CustTable))]
final class TutorialCustTable_Form_Extension
{
}

You may think that we need to add a chain of command class for the form control. However, in this case, we actually need to add form methods. Specifically, add an ‘init’ method and a lookup method. First, we will create a lookup method. And then the ‘init’ method, which gets called when the form first opens, will register our version of the lookup method.

First, add the lookup method. In this case, this is just a method that takes a form control object. It can actually be named something other than just ‘lookup’.

[ExtensionOf(formStr(CustTable))]
final class TutorialCustTable_Form_Extension
{
    public void overridenCustGroupLookup(FormStringControl _formControl)
    {
        SysTableLookup      sysTableLookup  = SysTableLookup::newParameters(tableNum(CustGroup), _formControl);

        // Add the lookup columns
        sysTableLookup.addLookupfield(fieldNum(CustGroup, CustGroup));
        sysTableLookup.addLookupfield(fieldNum(CustGroup, Name));
        sysTableLookup.addLookupfield(fieldNum(CustGroup, PaymTermId));

        // Run the lookup
        sysTableLookup.performFormLookup();
    }

}

Secondly, we need to extend the ‘init’ method to add code that will register the lookup method we just created and associate to the correct form control. The complete code should look like this.

[ExtensionOf(formStr(CustTable))]
final class TutorialCustTable_Form_Extension
{
    public void init()
    {
        next init();

        Posting_CustGroup.registerOverrideMethod(methodStr(FormDataObject, lookup), formMethodStr(CustTable, overridenCustGroupLookup));
    }

    public void overridenCustGroupLookup(FormStringControl _formControl)
    {
        SysTableLookup      sysTableLookup  = SysTableLookup::newParameters(tableNum(CustGroup), _formControl);

        // Add the lookup columns
        sysTableLookup.addLookupfield(fieldNum(CustGroup, CustGroup));
        sysTableLookup.addLookupfield(fieldNum(CustGroup, Name));
        sysTableLookup.addLookupfield(fieldNum(CustGroup, PaymTermId));

        // Run the lookup
        sysTableLookup.performFormLookup();
    }
}

In our example, the form control we needed to change the lookup for, had the ‘Auto Declaration’ property set to Yes. This meant we could simply use the name of the control, Posting_CustGroup, in our code. However, that is not always the case. If you need to reference a control that does not have this property set, you can find the control by adding an extra line of code. You still need to reference the control’s name. But this code still works even when the code does not have the ‘Auto Declaration’ property set to Yes. The ‘init’ method would then look like this.

public void init()
    {
        next init();

        FormStringControl custGroupControl = this.design().controlName(formControlStr(CustTable, Posting_CustGroup));
        custGroupControl.registerOverrideMethod(methodStr(FormDataObject, lookup), formMethodStr(CustTable, overridenCustGroupLookup));
    }

The method named registerOverrideMethod will tell the system what method to use as the lookup code for this form.

Demonstration

After adding all the code, save and compile your project.

In order to see your latest changes, reload the customer details form in your browser.

Finally, test out the overridden lookup method. You show now see all three columns showing in the lookup.

Conclusion

In this article you learned how to override an existing lookup method on an existing form. This is very useful for when you need to change the functionality on a form that is not part of your model. You learned how to override the lookup using chain of command. The lookup method is not the only form control method you can override on an existing form. Using these same techniques you can apply them to other situations.

Peter Ramer
Peter Ramer is a part of the Managed Application Services team at RSM working on Microsoft Dynamics 365. He focuses on the Retail and Commerce industries. When he is not solving problems and finding ways to accelerate his clients' business, he enjoys time with his three kids and amazing wife.

Share this:

2 thoughts on “How To Override An Existing Lookup Method: Chain Of Command

Add yours

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑