How to write clicked method for standard forms using event handlers.  

Scenario: 
When there is a requirement to create an object in standard form and implement a business logic on that object. In my case, I have created the button object, and I have used the ‘OnClicked’ event handler to perform the action. 

High level resolution steps 

Detailed resolution steps 

Step 1: Create a menu item object for the button in the action pane of the form. 

I have extended the standard CustTable form and added a button group and I have added menu item object for button inside the group. 

Navigation: AOT à CustTable à Create a form extension à Action pane à Button group à Events à ’On Clicked’ 

Clicked Method for Standard Forms using Event Handlers

Step 2: Add an event handler for the button through the class extension 

Created a class extension to perform form level operation and add the COC for form level, go to the particular form controller and choose the button which I want to add the business logic to and took the event handler from that button. 

Note: While creating a class make sure it’s in [ClassName_Extension] format. 

Navigation: Create a class extension àJobTable_Extension 

Step 3: Add business logic for the event handler 

In my case I have written the logic for ’On Clicked’ event, the button should be enabled only for the customers with Customer group ID =10. 

Code snippet:

[ExtensionOf(FormStr(CustTable))]
final class JobTable_Extension
{
    [FormControlEventHandler(formControlStr(CustTable, JobOrder), 
    FormControlEventType::Clicked)]
    public static void JobOrder_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        CustTable custTable;
        FormRun formRun = sender.formRun() as FormRun;
        FormDataSource fds = formRun.dataSource();

        custTable = fds.cursor();
        fds.formRun().design().controlName(formControlStr(CustTable, 
        JobOrder)).enabled(custTable.CustGroup == '10');
    }
}

Code Explanation : 

Output windows

In this scenario I have added the condition so that, button should be disabled for customers other than CustomerID = ‘10’.  

Navigation: Account Receivable → All Customers → Click on any one of the Customers  

In this scenario I have added the condition so that, button should be enabled for customers CustomerID = ‘10’.  

Navigation: Account Receivable → All Customers → Click on any one of the Customers  

Conclusion 

To add custom functionality to a standard form in Dynamics 365 for Finance and Operations, creating a button and using the ‘OnClicked’ event handler is a common approach. 

Using this method allows us to trigger specific actions via the UI, while ensuring the backend 

Leave a Reply

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