Run a workflow using JavaScript in CRM 2011

Run a workflow using JavaScript in CRM 2011

Here is the code to run workflow using JavaScript

function RunWorkFlow() {
    var entityId = Xrm.Page.data.entity.getId().substring(1, 37); //Guid of record that workflow is to run on.
    var workflowId = “090227fa-68ad-453b-8ea5-3ca63058d240”; //Workflow Guid.
    var url = Xrm.Page.context.getServerUrl();
    var OrgServicePath = “/XRMServices/2011/Organization.svc/web”;
    url = url + OrgServicePath;
    var request;
    request = +
              +
                “” +
                    “” +
                    +
                        “EntityId” +
                        “” + entityId + “” +
                         +
                        “WorkflowId” +
                        “” + workflowId + “” +
                    ““ +
                    +
                    “” +
                “” +
                +
            ;
    var req = new XMLHttpRequest();
    req.open(“POST”, url, true)
    // Responses will return XML. It isn’t possible to return JSON.
    req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);
    req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
    req.onreadystatechange = function () { assignResponse(req); };
    req.send(request);
}
function assignResponse(req) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            //alert(‘successfully executed the workflow’);
        }
    }
}
Happy CRM’ing

Gopinath

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