Extension of a form control method - Chain of command- D365 F&O - X++ Code

 


Extension of a form control method - Chain of command- D365 F&O - X++ Code

Chain of command is an option that helps you enhance the functionality of a form control 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 clicked() method of the form button control:


[ExtensionOf(formControlStr(CustTable, DirPartyEntityAssociationUpdate))]
final class CustTableFormControlDirPartyEntityAssociationUpdate_Extension
{
    public void clicked()
    {
        //code
        next clicked();
        //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 CustTableFormControlDirPartyEntityAssociationUpdate_Extension class extends the DirPartyEntityAssociationUpdate form control class for the CustTable form, as specified by the formControlStr(CustTable, DirPartyEntityAssociationUpdate) function call.

The clicked() method is a built-in X++ method that is called when the button or menu item that corresponds to the form control is clicked. The next clicked() statement in the clicked() method is used to call the clicked() method of the parent class, which in this case is the DirPartyEntityAssociationUpdate form control class.

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

Here's an example of how you might use ExtensionOf and next clicked() to extend the functionality of the DirPartyEntityAssociationUpdate form control on the CustTable form:


[ExtensionOf(formControlStr(CustTable, DirPartyEntityAssociationUpdate))]
final class CustTableFormControlDirPartyEntityAssociationUpdate_Extension
{
    public void clicked()
    {
        // Call the parent class's clicked() method first
        next clicked();

        // Add custom clicked code here
        info("DirPartyEntityAssociationUpdate button has been clicked.");
    }
}

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

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


Comments