Invoke Power Automate from Dynamics 365 Or Model Driven Apps using Form Scripts

In this blog post, we will Invoke a Power Automate from Dynamics 365 Or Model-Driven Apps using Form Scripts.

Scenario

A person fills up the Form and saves it in the Model-Driven app. The Script captures the items(column) values, passes the values to Power Automate which generates a PDF, and sends it as an Email Attachment.

This article is broadly devide into two main part

  • Configuring Form Scripts in model-driven apps.
  • Invoking a Power Automate using Form Scripts.

Basics of Client API object model for Model-Criven apps

The Client API object model for model-driven apps provides you objects and methods that you can use to apply custom business logic in model-driven apps using JavaScript, such as Get or set column values, show and hide user interface elements, etc. For details on the Client API, object model see the link.

We will Trigger our code whenever a Form is saved(OnSave). We will execute our JavaScript code by associating it with an event so that it is executed when the event occurs. For details on client-side events see the link.

Before we begin, please make sure the following prerequisites are in place

  • Office 365 subscription with SharePoint, Power Automate and Exchange license.
  • Muhimbi PDF Converter Services Online Free, full or trial subscription (Start trial).
  • Appropriate privileges to create Power Automate Flows.
  • Working knowledge and Appropriate privileges to Dynamics 365/Model Driven Apps.
  • Basic understand of JavaScript programming language.

Configure Power Automate to receive inbound requests over HTTPS

Before we build our Power Automate(Flow), let have a quick look at the Form.

We need to export all the item values(columns). The Sample JSON with reference to the above form would be –

{
"Name":"Clavin's Anniversary",
"Type":"Anniversary",
"PartyTime":" Fri Dec 24 2021 08:00:00 GMT+0530 (India Standard Time)",
"PartyLocation":"Dominos",
"Organizer":"Clavin"
}

Step 1 – For this demo, we will use the Power Automate built-in Request trigger, which will help us receive inbound requests over HTTPS.

  • Add the When an HTTP request is received trigger.
  • Click on  When an HTTP request is received schema link and paste the above JSON and click on Done.

Step 2 – Add a Compose action and paste the HTML below.

<html>
<body>
<h1>Its Party Time </h1>
<h2>Event Name "@{triggerBody()?['Name']}"</h2>
<h2>Event Type "@{triggerBody()?['Type']}"</h2>
<h2>Party Time " @{triggerBody()?['PartyTime']}"</h2>
<h2>Party Location "@{triggerBody()?['PartyLocation']}"</h2>
<h2>Organizer "@{triggerBody()?['Organizer']}"</h2>
</body>
</html>

Note – You will need to change the fields in the HTML

Step 3 – Add the Muhimbi Convert HTML to PDF action and pass it the Output of the Compose action.

Step 4 – Add the “Send an email(v2)” action and configure it with reference to the instructions below

  • To: Email of HR person
  • Subject: Party for @{triggerBody()?[‘Name’]}}
  • Body: Email Body.
  • Attachments Name – 1: Name.pdf the output of the Trigger
  • Attachments Content -1: “Processed File Content” the output of Convert HTML to PDF action.

Step 5 – Give your Flow a meaningful name and Save it.

  • Copy the URL from the Trigger, we will use in our Form Script.

Configure Forms Scripts in Model-Driven apps

Step 1 – The below Script reads the item value(columns) from the Form and Triggers a Power Automate.

function alertHR(executionContext)
{
    var formContext = executionContext.getFormContext();
    var accountName = formContext.getAttribute("clav_name").getValue();
    var type = formContext.getAttribute("clav_type").getText();
    var partyTime = formContext.getAttribute("clav_dateandtimeofparty").getValue();
    var partyLocation = formContext.getAttribute("clav_locationforparty").getText();
    var organizelook =  formContext.getAttribute("clav_organizer").getValue();
    var organize = organizelook [0].name;

    var req = new XMLHttpRequest();

debugger;
    var params = {
	"Name":accountName,
	"Type":type,
	"PartyTime":tring(partyTime,
	"PartyLocation":organize,
	"Organizer":req
}

//your Power Automate request URL
var url = "https://prod-64.westus.logic.azure.com:443/workflows";

//send https request to Power Automate
        req.open("POST", url, true);
        req.setRequestHeader('Content-Type', 'application/json');
        req.send(JSON.stringify(params));
        Xrm.Utility.alertDialog(accountName + type + partyTime + partyLocation + organize);
}

Step 2 – Add event handler function using the User-Interface.

  • Go to Power Apps.
  • In the left navigation pane, select Data and then select Tables.
  • From the list of tables, select the table where you want to add the event handlers.
  • On the Form Screen click on the Form libraries, click on + Add library and + New web resource.
  • On the New web resource page, add the Display name and Name, Type JavaScript (JS) and Click on Save.
  • Select Events tab. Select on the On Save Event type, select the Library, Function name, check the Enabled and Pass execution context as the first parameter and click on Done.
  • Save and Publish the Form.

All Done! Navigate back to your Model-Driven APP, fill in the Form, and click on Save. After a few seconds, you should have an email with a PDF attachment.

4 thoughts on “Invoke Power Automate from Dynamics 365 Or Model Driven Apps using Form Scripts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s