Hacking your way to Execute a CRM workflow programmatically from anywhere in Dynamics 365 portals

Before I even proceed further, couple of things. You might be thinking calling On-Demand workflows can be so sweetly done from entity list and entity forms just using configuration. Then why the hell  we need to hack?

Well I am aware of that wonderful feature but this post is not about that. You can call an on-demand workflow from entity form and entity list but again, we need an entity form or list right?

What if your customer has spun up some custom buttons on the fly using script and click of the buttons they want to perform some server side operations? What if the area may be an entity form or entity list and just some other area of the portal like a registration page or something like that?

Well my customer had a similar sort of requirement. And sometimes no matter how much you try, you may need to give in to the customer demands.

Please understand that I have hacked around the platform code a bit to get this working. It may continue working in the future or may stop if some changes in the platform and I have put a caveat for my customer already. However if you are in dire straits like me, may be you can do this but make sure you put a caveat to your customer.

I guess half of the readers have left after those big red comments. However if you are still reading, may be you would like to continue till the end.

So I created a workflow for account entity which does something mundane – “Create a Task record”.

image

Now I want to call this workflow from anywhere and not just an entity list record or entity form. Well if that the case below the code to execute this workflow. I have put it inside the function executeCustomWorkflow which you can call from anywhere in portals and it just works.

function executeCustomWorkflow() {
    debugger;
    var u = "/_services/execute-workflow/7b138792-1090-45b6-9241-8f8d96d8c372"; // execute workflow request url

    var params = {};
    var workflow = {};

    workflow.LogicalName = "workflow";
    workflow.Id = "43459d81-1cfe-4465-b9ed-e756d00adb69"; // guid of the workflow you wish to execute. This will be consistent across your environments

    var data = {};
    data.LogicalName = "account";
    data.Id = "b5233ccc-8540-e911-a837-000d3a11e59b"; // 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;
    });
}

I have put appropriate comments wherever required so that you can understand it. Now this function can be bound to button click or any other event and it will work.

But as I always try, I would not leave you in the dark. You may be wondering, how did I get the Workflow execute request URL? If you configure a workflow button for entity form and see it’s HTML, you will find the URL right up there in the mark-up.

image

What power does it give you then? Well it opens up loads of things and also addresses the perennial problem of performing CRUD operations from custom buttons from anywhere in the portal on-demand.

All you end is an entity on which the workflow would be called. And you can spin up just a dummy entity for invocation purpose and do the rest in workflow.

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)

Advertisements

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s