Thursday 12 January 2023

Dynamics 365 – Call Power Automate Flow from JavaScript

 In this post I will show the steps to call Cloud(Power Automate) flow from Javascript. I will send a JSON request body to my flow and get a JSON response body back. We will do that in 3 simple steps. Let’s start.

Step 1 – Create a Power Automate Flow

Go to https://make.powerautomate.com/ and choose your environment that you want your flow to run. Select Create from the left navigation. Select “Instant cloud flow” and choose “When a HTTP request is received” as your trigger.

To send data to your flow, you need to generate your input schema. If you don’t have a schema, the flow trigger generates you one based on your sample payload. Expand your trigger action and click “Use sample payload to generate schema” and fill with your sample json string that you will send later.

To get a response back from your flow, add a “Response” action to your flow and fill “Status Code“, “Headers” and “Body as you desire. The data you put here will return you as response later in javascript code.

Step 2 – Copy Your “HTTP POST URL”

When you save your flow, your trigger action will generate a “HTTP POST URL” to call your flow. We will use that URL to send POST request from our code.

STEP 3 – Configure the Javascript Code

You can use the below code to call your flow. Don’t forget to change flowURL and request body ðŸ˜‰ . We use “JSON.parse(body)” to convert our JavaScript object to JSON string. The XMLHttpRequest sends the request as async and get the response back. You can read the response with “this.response” parameter.

function callFlow(flowURL) {
let parameter1 = "parameter1";
let parameter2 = "parameter2";
let body = {
"Parameter1": parameter1,
"Parameter2": parameter2
};
let req = new XMLHttpRequest();
req.open("POST", flowURL, true);
req.setRequestHeader("Content-Type", "application/json");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
let resultJson = JSON.parse(this.response);
} else {
console.log(this.statusText);
}
}
};
req.send(JSON.stringify(body));
}

Hide New... button on lookup controls in model-driven apps

  The 'New ...' button is shown upon opening the lookup search dialog whenever the logged in user has at least user create privileg...