Execute a CRM(2011/2013) OnPremesis or Online Workflow from Javascript



The function below can be used to execute a workflow from JavaScript using the Organization.svc service instead of the CrmService.asmx from previous versions (CRM 3.0 and CRM4.0 versions): The first parameter of the function is the workflowid that it will be called, and the second parameter is only to show or not a window to confirm the action that will be made in next: function ExecuteWorkflow(workflowId, confirmQuestion) { var _return = true; if(confirmQuestion) _return = window.confirm(‘Are you want to execute workflow.’); if (_return) { var url = Xrm.Page.context.getServerUrl(); var entityId = Xrm.Page.data.entity.getId(); var OrgServicePath = “/XRMServices/2011/Organization.svc/web”; url = url + OrgServicePath; var request; request = “<s:Envelope xmlns:s=”http://schemas.xmlsoap.org/soap/envelope/”>&#8221; + “<s:Body>” + “<Execute xmlns=”http://schemas.microsoft.com/xrm/2011/Contracts/Services&#8221; xmlns:i=”http://www.w3.org/2001/XMLSchema-instance”>&#8221; + “<request i:type=”b:ExecuteWorkflowRequest” xmlns:a=”http://schemas.microsoft.com/xrm/2011/Contracts&#8221; xmlns:b=”http://schemas.microsoft.com/crm/2011/Contracts”>&#8221; + “<a:Parameters xmlns:c=”http://schemas.datacontract.org/2004/07/System.Collections.Generic”>&#8221; + “<a:KeyValuePairOfstringanyType>” + “<c:key>EntityId</c:key>” + “<c:value i:type=”d:guid” xmlns:d=”http://schemas.microsoft.com/2003/10/Serialization/”>&#8221; + entityId + “</c:value>” + “</a:KeyValuePairOfstringanyType>” + “<a:KeyValuePairOfstringanyType>” + “<c:key>WorkflowId</c:key>” + “<c:value i:type=”d:guid” xmlns:d=”http://schemas.microsoft.com/2003/10/Serialization/”>&#8221; + workflowId + “</c:value>” + “</a:KeyValuePairOfstringanyType>” + “</a:Parameters>” + “<a:RequestId i:nil=”true” />” + “<a:RequestName>ExecuteWorkflow</a:RequestName>” + “</request>” + “</Execute>” + “</s:Body>” + “</s:Envelope>”; 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.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute&#8221;); req.onreadystatechange = function () { assignResponse(req); }; req.send(request); } } function assignResponse(req) { if (req.readyState == 4) { if (req.status == 200) { alert(‘Successfully executed the workflow’); } } }

#Execute #CRM2013 #Workflow #Javascript #CRM2011

0 views0 comments

Recent Posts

See All