Quantcast
Channel: Sam's Activities
Viewing all articles
Browse latest Browse all 3363

Execute Workflow using Web API in Dynamics 365

$
0
0

Introduction:

In every release, Microsoft comes up with some new and interesting features. With this Dynamics 365 release, the WEB API platform has been enhanced further. Microsoft has introduced many actions using Web API. Here we will see one of those new actions introduced in Dynamics 365 i.e. “ExecuteWorkflow”.

Until Dynamics CRM 2016 Update 1 (8.1), ExecuteWorkflowRequest was not available using Web API. But with Dynamics 365 “ExecuteWorkflow” is now available to us. Now we can execute the workflow using Web API.

Below is the code using which you can execute workflow.

function executeWorkflow(accountId, workflowId, clientUrl) {
    var functionName = "executeWorkflow >>";
    var query = "";
    try {

        //Define the query to execute the action
        query = "workflows(" + workflowId.replace("}", "").replace("{", "") + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";

        var data = {           
                "EntityId": accountId                         
        };

        //Create request
       // request url
       //https://org.crm.dynamics.com/api/data/v8.2/workflows(“f0ca33cc-23fd-496f-80e1-693873a951ca”)/Microsoft.Dynamics.CRM.ExecuteWorkflow
        var req = new XMLHttpRequest();
        req.open("POST", clientUrl + "/api/data/v8.2/" + query, true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");

        req.onreadystatechange = function () {

            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null;

                if (this.status == 200) {
                    //success callback this returns null since no return value available.
                    var result = JSON.parse(this.response);


                } else {
                    //error callback
                    var error = JSON.parse(this.response).error;
                }
            }
        };
        req.send(JSON.stringify(data));

    } catch (e) {
        throwError(functionName, e);
    }
}

Conclusion

There are a lot of cases where you add ribbon button to personalize the user experience but behind the scene you are actually executing a workflow. You can now go ahead and get those done through the “ExecuteWorkflow” action available. We will explore the other actions in later blogs.

Now u can try User Adoption Monitor from Microsoft AppSource– Try today!


Viewing all articles
Browse latest Browse all 3363

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>