AX / D365FO – Get DataSource in a Form Control EventHandler Method

In Dynamics 365 for Operations you can react to the OnClicked event by copying the event handler method for the event and pasting the method into a class.

Below is an example of an event handler method that reacts to the OnClicked event of a button on a form

Create new Class and paste below code in it for SalesEditLines EventHandlers

[FormControlEventHandler(formControlStr(SalesEditLines, OK), FormControlEventType::Clicked)]
    public static void OK_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        Args args = new Args();
        FormCommandButtonControl  callerButton = sender as FormCommandButtonControl;
//Retrieves the button that we're reacting to
        FormRun form = callerButton.formRun(); //Gets the running SalesEditLines form

//Get the salesId that was selected in the SalesEditLines form
        FormDataSource salesParmTable_ds = form.dataSource(formDataSourceStr(SalesEditLines, SalesParmTable)) as FormDataSource;
        SalesParmTable salesParmTable = salesParmTable_ds.cursor();
        SalesTable salesTable=salesParmTable.salesTable();

        if(salesTable.SalesStatus==SalesStatus::Invoiced)
        {
            //Any Action
        }
    }

Another Sample for OnModified of check box control – AllowEdit text box control based on checkbox selection

 [FormControlEventHandler(formControlStr(EcoResProductDetailsExtended, Other_CatchWeight), FormControlEventType::Modified)]
    public static void Other_CatchWeight_OnModified(FormControl sender, FormControlEventArgs e)
    {
        FormCheckBoxControl  callerButton = sender as FormCheckBoxControl ;  //Retrieves the button that we're reacting to
        FormRun element = callerButton.formRun();
        FormControl catchweightMultipleToSubProject = element.design(0).controlName("Other_CatchweightMultiple");
        if(callerButton.checked())
        {
            catchweightMultipleToSubProject.allowEdit(true);
        }
        else
        {
            catchweightMultipleToSubProject.allowEdit(false);
        }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s