Sending Form Data from form in D365FO Using a Button Click

In this blog post, I’ll walk you through how to implement a button click handler in Dynamics 365 Finance and Operations (D365FO) to send data to an external API. 

Requirement

I had a requirement where I needed to send the selected worker's data to an external API when a button is clicked on the HcmWorkerV2 form.

To achieve this, I used a form control event handler that is triggered when the custom button is clicked.

Implementation

Here’s the X++ code that handles the button click:

[FormControlEventHandler(formControlStr(HcmWorkerV2, SendApiData), FormControlEventType::Clicked)]

public static void SendApiData_OnClicked(FormControl sender, FormControlEventArgs e)

{

    FormCommandButtonControl callerButton = sender as FormCommandButtonControl;

    FormRun form = callerButton.formRun();

    // Get the form datasource and current worker record

    FormDataSource hcmWorker_ds = form.dataSource(formDataSourceStr(HcmWorkerV2, HcmWorker)) as FormDataSource;

    HcmWorker hcmWorker = hcmWorker_ds.cursor();

    if (hcmWorker)

    {

        // Send the selected worker data to API

        EmployeeServiceAPI::sendDataToApi(hcmWorker);

    }

}

Comments

Popular posts from this blog

JSON-Based HTTP POST Request from X++ to Retrieve an Access Token

Calling an External API from D365FO Using X++ to Retrieve a Token

Restricting Date Selection in Dynamics 365 FO using X++