web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Execute workflow using javascript in CRM 2013

(0) ShareShare
ReportReport
Posted on by

function OK() {
var entityId = Xrm.Page.data.entity.getId();
var workflowId = 'C652E4A2-3187-4526-8CA4-A4A4FD8F9EC3';
//Function for executing the workflow
if (entityId != null) {
startWorkflow(entityId, workflowId);
}
}

function startWorkflow(entityId, workflowProcessId) {
try {
var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
var serverUrl = "";
if (typeof GetGlobalContext == "function") {
var context = GetGlobalContext();
serverUrl = context.getServerUrl();
}
else {
if (typeof Xrm.Page.context == "object") {
serverUrl = window.location.protocol + "//" + window.location.host + "//" + Xrm.Page.context.getOrgUniqueName();
//alert(serverUrl );
// serverUrl = Xrm.Page.context.getServerUrl();
//alert(serverUrl );
}
else { throw new Error("Unable to access the server URL"); }
}
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length-1);
}

serverUrl = serverUrl + OrgServicePath;
var requestMain = ""
requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";
requestMain += " <s:Body>";
requestMain += " <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";
requestMain += " <request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";
requestMain += " <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>EntityId</c:key>";
requestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">" + entityId + "</c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>WorkflowId</c:key>";
requestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">" + workflowProcessId + "</c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " </a:Parameters>";
requestMain += " <a:RequestId i:nil=\"true\" />";
requestMain += " <a:RequestName>ExecuteWorkflow</a:RequestName>";
requestMain += " </request>";
requestMain += " </Execute>";
requestMain += " </s:Body>";
requestMain += "</s:Envelope>";

var req = new XMLHttpRequest();
req.open("POST", serverUrl, false)
// 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.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&amp;#8221");
//req.onreadystatechange = function () { };
req.send(requestMain);

alert("Workflow execution Completed.");
}
catch (e) {
alert(e.message.toString());
}

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hey there,

    did you face any issue? or its a knowledge sharing?

  • Community Member Profile Picture
    on at

    I am facing the problem and 1-2 expand R&D for this problem

    crm details

    Version 2013 On Premise

    Need:Execute workflow using Javascript

    please provide best solution

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
  • Community Member Profile Picture
    on at

    Dear Sir

    i am using same code but occur Error 500 Interonal Server error

  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    on at
  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    on at

    Also refer the following thread:

    community.dynamics.com/.../143712

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans