Execute a workflow programmatically from PowerApps Portals/ Dynamics 365 workflows

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com

This one is interesting. And I have actually written a post about a year back on the same topic. But with time, some of the methodologies mentioned in the post no longer work. Hence kind of re-writing this one for support with Power Apps portals.
Before I go ahead and explain how I do it, a quick note on this. This is not a OOB feature. In other words Microsoft haven’t introduced an API yet to call an on-demand workflow from PowerApps portals. It’s a workaround using plain simple JavaScript techniques. So let’s see how we can accomplish them.

We all know that you can configure On-demand workflows to run on entity forms or for an item in Entity List. In the below screenshot, I have configured an on-Demand workflow to run for run for each account record in an entity list.
image
Not a big deal. Anyone can configure it because it’s quite easy. However in this kind of configuration, the portal user have to manually run the workflow. What if the workflow need to run automatically based on certain business logic? No user interaction. Just when certain conditions are met, the workflow will be invoked programmatically. So let’s see how we can do it.
The first step is to explore how portal is invoking it. I launch developer tools. And below is HTML for the “Run Workflow” link.
image
For invoking it programmatically, we would need the below information.

  • Url – Take the value specified in data-url property. Here the value is “/_services/execute-workflow/f46b70cc-580b-4f1a-87c3-41deb48eb90d”.
  • workflowid – Take the value in data-workflowid attribute. Here the value is fb51aff2-d334-42f0-9595-fffc063364e6.

Now since this a workflow on Account enity, get the GUID of the account record for which the workflow would run. Very important thing to note here is the workflow url. You should not be using the url being mentioned in the blog. Rather inspect your HTML and you the url specified in the data-url HTML attribute.


We are all set. Below is the code to execute the workflow.

function executeWorkflow() {
    debugger;
    var u = "/_services/execute-workflow/f46b70cc-580b-4f1a-87c3-41deb48eb90d"; // execute workflow request url
    var params = {};
    var workflow = {};
   workflow.LogicalName = "workflow";
    workflow.Id = "fb51aff2-d334-42f0-9595-fffc063364e6"; // guid of the workflow you wish to execute. This will be consistent across your environments
   var data = {};
    data.LogicalName = "account";
    data.Id = "d4cf96bd-4ef9-e911-a813-000d3af02cd4"; // guid of the account record.
   params.workflow = workflow;
    params.entity = data;
   var jData = JSON.stringify(params);
   // calling the platform method. bascially you can do your own promise and invoke the execute workflow url.
    shell.ajaxSafePost({
       type: "POST",
       contentType: "application/json",
       url: u,
       data: jData
    }).done(function (r) {
       debugger;
    }).fail(function (n) {
       debugger;
    });
}

 
 
If you see in the above code, I have used the platform function, shell.ajaxSafePost. Using custom HTTP Request does not seem to make the call.
Hope this helps!
Debajit Dutta
(Business Solutions MVP)
 

2 thoughts on “Execute a workflow programmatically from PowerApps Portals/ Dynamics 365 workflows”

  1. what is shell.ajaxSafePost.
    Need more details. I do not have much power apps experience but willing to learn.

Comments are closed.