Extension of a form data source field method - Chain of command- D365 F&O - X++ Code


Extension of a form data source field method - Chain of command- D365 F&O - X++ Code

Chain of command is an option that helps you enhance the functionality of a form data source field method. This option is applicable for standard methods and custom methods of the form.

You can use the following code pattern if you need to extend the lookup() method of the form data source fields:


[ExtensionOf(formDataFieldStr(CustTable, CustTable, CustGroup))]
final class CustTableFormDataFieldCustGroup_Extension
{
    public void lookup(FormControl _formControl, str _filterStr)
    {
        //Code
        next lookup(_formControl, _filterStr);
        //Code
    }
}

In X++, the ExtensionOf keyword is used to create a class extension that extends the functionality of an existing class. In this case, the CustTableFormDataFieldCustGroup_Extension class extends the CustTable form data field class for the CustGroup field, as specified by the formDataFieldStr(CustTable, CustTable, CustGroup) function call.

The lookup() method is a built-in X++ method that is called when a lookup form is opened for the data field. The next lookup() statement in the lookup() method is used to call the lookup() method of the parent class, which in this case is the CustTable form data field class.

By using next lookup() to call the parent class's lookup() method, any lookup functionality in the parent class is executed before the lookup functionality in the CustTableFormDataFieldCustGroup_Extension class. This ensures that any necessary lookup tasks are completed before the extension's code is executed.

Here's an example of how you might use ExtensionOf and next lookup() to extend the functionality of the CustGroup field on the CustTable form:


[ExtensionOf(formDataFieldStr(CustTable, CustTable, CustGroup))]
final class CustTableFormDataFieldCustGroup_Extension
{
    public void lookup(FormControl _formControl, str _filterStr)
    {
        // Call the parent class's lookup() method first
        next lookup(_formControl, _filterStr);

        // Add custom lookup code here
        info("CustGroup lookup has been opened.");
    }
}

In this example, we define the CustTableFormDataFieldCustGroup_Extension class as an extension of the CustTable form data field class for the CustGroup field. In the lookup() method, we call the parent class's lookup() method using next lookup(), and then we add our own custom lookup code, which simply displays an information message.

By using class extensions and the next lookup() statement, we can easily extend the functionality of existing X++ classes, while still ensuring that any necessary lookup tasks are completed correctly.


Comments