Execute Workflow using Web API in Dynamics 365

The following JavaScript code will be used to execute workflows in Dynamics 365.
Note: The following code will work only for Dynamics CRM 2016 Update 1 (8.1) and later releases including Dynamics 365.

function ExecuteWorkflow() {
var correspodenceId = Xrm.Page.data.entity.getId(); // case guid
var workflowId = "{2e7dca90-3a40-1234-3456-a8e3fc1edcd2}"; //Replace your workflow id here. workflowId is fixed because of solution transfer and it is not going to change with environment.
var functionName = "ExecuteWorkflow: "; //function name
var query = "";
try {
//Define the query to execute the worklfow
query = "workflows(" + workflowId.replace("}", "").replace("{", "") + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
var data = {
"EntityId": correspodenceId
};
//Create a request
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + query, false);
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) {
    alert('An error has occurred - Function name:' + functionName + 'Error:' + e) //throw exception
}
}

Originally posted in Sandeep Kolarveettil’s blog: Read more

0 Comments

Leave a reply

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

©2019 Dynamics 365 Global Community

Log in with your credentials

or    

Forgot your details?

Create Account

Skip to toolbar