Recently we needed to trigger Power Automate flow from Javascript later after reading my friend's Blog (Ajit Patra Link below) we decided to have a Button which will execute power automate flow using JavaScript.
It was presented as On-demand Power Automate flow to System user
Please go through Referred Ajit’s Blog for more detail, where he explained how to Execute PowerAutomate on click of a button on the Lead record
Below is Demonstrated in below part
- 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
Step 1 Create Power automate Flow to receive HTTPS Request from Javascript and send Email
We will create Power automate flow with the trigger “When an HTTP request is received” this will provide us an URL which we will use in our js for execution.
Below are the steps Performed
- Create An Power Automate flow, with the trigger ” When an HTTP request is received”
- Use Sample payload to generate the schema , this Schema will be used as Parameter when executing Power automate
-
Schema generated are available in Actions to be used as dynamic Values
- Now copy the Http Post URL from HTTP action, Also Url will only be available once power automate is saved
Step 2 Register Javascript to Execute Https Request from Microsoft dynamics 365
We will create a simple JS web resource and will use below Javascript Provided by Ajit on his blog: Click Here
- Create New Webreource and use blow Js Code
function executeRequest(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); }
- Save the javascript and add it under the entity Contact
Step 3 Creating a Ribbon Button on Contact Entity and add javascript action to the button Command
- Create a new Solution and add the entity where the button needs to be added “testcontact”
- Now open the ribbon workbench, If ribbon workbench is not installed, Install Ribbon Workbench from here: Click Here
- Once successfully imported, Open the ribbon workbench and select the Solution created in 1st step
- Once loaded we will find 3 Ribbon HOME, SUBGRID, and FORM
- Now Drag and drop the button to FORM Ribbon
- Give Label, Icon, etc. NOTE IN UCI SVG FORMAT IS SUPPORTED
- Now Click on Add new command, Rename it and Click on ADD ACTION
- Now search Javascript registered in step 2 and mention function name
function executeRequest(executionContext) { var formContext = executionContext.getFormContext(); var flowUrl = "Http Post URL"; --------------------------------------------- --------------------------- --------------------------------------------
- Most important is PrimaryControl should be passed in Parameter
Thanks