How to call (invoke) Power Automate Flows on an action via HTTP Request in Business Central Using AL Code
WebSiteAdmin, July 30, 2024892 Views
Microsoft Dynamics 365 Business Central is a powerful Enterprise Resource Planning (ERP) solution that offers a wide range of customization options to meet the unique needs of your business. One such customization is to call (invoke) Power Automate Flows on an action via HTTP Request in Business Central Using AL Code. Integrating Power Automate Flows with Business Central via HTTP requests using AL code provides a powerful mechanism for automating business processes and enhancing productivity.
Requirement:
We recently encountered a requirement where we need to trigger (invoke) a Power Automate Flow when a user clicks on a specific action within Business Central.
Prerequisites:
- Access to a Dynamics 365 Business Central instance with administrator privileges.
- Basic knowledge of AL (Application Language) code.
- Visual Studio Code with AL Language extension installed.
- Power Automate Flow that contains an HTTP trigger to receive the request.
- The HTTP URL of the Power Automate Flow that you want to invoke.
Solution (Implementation Steps):
In Dynamics 365 Business Central, follow these steps to call (invoke) Power Automate Flows on an action via HTTP Request:
Step 1: Create the Action in AL
Define an action within the AL codebase. This action contains a trigger named OnAction() which executes when invoked. Within this trigger, an HTTP request is crafted to call the designated Power Automate Flow.
action("Action") { ApplicationArea = All; Image = SelectMore; Promoted = true; PromotedCategory = Process; trigger OnAction() var HttpContent: HttpContent; HttpClient: HttpClient; HttpHeaders: HttpHeaders; HttpRequestMessage: HttpRequestMessage; HttpResponseMessage: HttpResponseMessage; Content: Text; GetEmailId_From_Current_User: Text; responseText: Text; No: Text; Var_UserId: Text; UserTable: Record User; begin // Get the current user's ID Var_UserId := UserId; // Find the user record based on the user ID UserTable.SetRange("User Name", Var_UserId); if UserTable.FindSet() then begin // If user record found, get the email ID associated with the current user GetEmailId_From_Current_User := UserTable."Authentication Email"; end; // Construct JSON content to send in the HTTP request Content := '{ "No.": "' + No + '" , "EmailId": "' + GetEmailId_From_Current_User + '"}'; // Write content to HttpContent object HttpContent.WriteFrom(Content); // Get headers for the HTTP request HttpContent.GetHeaders(HttpHeaders); // Clear existing headers HttpHeaders.Clear(); // Add Content-Type header HttpHeaders.Add('Content-Type', 'application/Json'); // Set the content and URI for the HTTP request message HttpRequestMessage.Content := HttpContent; HttpRequestMessage.SetRequestUri('HTTP_URL_of_Power_Automate_Flow'); // Set the HTTP method to POST HttpRequestMessage.Method := 'post'; // Send the HTTP request HttpClient.Send(HttpRequestMessage, HttpResponseMessage); // Read the response content HttpResponseMessage.Content().ReadAs(responseText); end; }
Explanation:
- Inside the OnAction() trigger, the code initializes variables necessary for handling HTTP requests and responses. It retrieves the current user’s ID (UserId) and seeks the corresponding user record in the User table. If found, the authentication email associated with the user is retrieved.
- Next, the code constructs JSON content (Content) comprising a “No.” field and the user’s authentication email. This content is then written into an HttpContent object. HTTP headers are configured, specifying the content type as JSON.
- An HttpRequestMessage is then prepared with the content and the URI of the target endpoint, which is the HTTP URL of the Power Automate Flow. A POST request is dispatched to this URI using an HttpClient.
- Finally, the response from the server is read and stored in responseText.
Step 2: Replace HTTP URL of Power Automate Flow
Replace the placeholder ‘HTTP_URL_of_Power_Automate_Flow’ in the AL code with the actual HTTP URL of the Power Automate Flow to be invoked.
Step 3: Publish and Install the Extension
Publish the AL code as an extension and install it in your Dynamics 365 Business Central instance.
Step 4: Test the Customization
Trigger the action within Business Central to invoke the Power Automate Flow via HTTP request. Verify the execution and response to ensure successful integration.
Conclusion:
Integrating Power Automate Flows with Dynamics 365 Business Central through HTTP requests using AL code facilitates streamlined automation of business processes. By following the outlined steps, businesses can efficiently orchestrate workflows, thereby enhancing productivity and agility in their operations.