Scenario is
1. "Execute Flow button" is Clicked from CRM
2. JS will execute Power Automate Flow using XMLHttpRequest
3.. Power Automate is Configured to send Response with Status 200 if condition fail or successful.
4. Js receives the Response and shows message as alert.
and all these activities will happen realtime
The post is Divided into 2 Parts
- Executing PowerAutomate Flow from Button using javascript
- Configuring response action in Power automate and Capturing response message in CRM using javascript
Step1 : Executing PowerAutomate Flow from Button using javascript
We will action “When an Http Request is received ” and follow below steps
- Create Power automate Flow to receive HTTPS Request from Javascript and send Email
- Register Javascript
- Creating a Ribbon Button on Contact Entity and add javascript action to the button Command and pass PrimaryControl
As in our previous post we already mentions steps to register and use it in Javascript
Below code open request to execute power automate flow, Next step is to see how we can get Response message in JS
function readXML(executionContext) { var formContext = executionContext.getFormContext(); var flowUrl = "Http Post URL"; var input = JSON.stringify({ "contactid": formContext .data.entity.getId().replace("{", "").replace("}", "") }); var req = new XMLHttpRequest(); req.open("POST", flowUrl, true); req.setRequestHeader('Content-Type', 'application/json'); req.send(input); }
Step2 : Add response Action in Power Automate Flow
In Step1, we had executed XMLHttpRequest and have not written code to get a response in JS, In this step we will add custom response message which we will get in Javascript .
- Adding action “when a HTTP request is received”, setup payload
- Adding Condition “1 = 1”
- Adding Response action with Status = 200 and Body with Message “Yes Condition”
Step3 : Get Response in Javascript
We have requested XMLHttpRequest to Execute Flow, Adding custom response msg in Power Automate,
Now we will use blow code to get response with status 200 in CRM
function readXML(executionContext) { var formContext = executionContext.getFormContext(); var flowUrl = "Http Post URL"; var input = JSON.stringify({ "contactid": formContext .data.entity.getId().replace("{", "").replace("}", "") }); var req = new XMLHttpRequest(); req.open("POST", flowUrl, true); req.setRequestHeader('Content-Type', 'application/json'); ////Response req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = this.response; alert("" + result); } else if(this.status === 400){ alert(this.statusText); var result = this.response; alert("Error" + result); } } }; ////End req.send(input); }
We can write the code to get request and get response Asynchronous or Synchronous Thanks