How to trigger the workflow using javascript in Dynamics 365?

Trigger the workflow

The report introduces an architectural framework for the topic - “how to trigger the workflow using java script in Microsoft Dynamics CRM”. We have completed this research during one of our projects and we strongly believe that our practical solution will be helpful for the programmers dealing with Microsoft Dynamics CRM development world. We believe in sharing quality information with potential profiles already working on Microsoft Dynamic CRM platform.

Undoubtedly, real time workflows are a powerful functionality in Microsoft Dynamics CRM 2013. As a programmer, you can now choose to work between workflows or plug-ins for plenty of synchronous operations. We have compiled an easy solution for better understanding about the workflows in Microsoft dynamics CRM 2013.

We are introducing how to Trigger the workflow using Java script in Microsoft Dynamics CRM. To implement java script to trigger workflow do the following steps.

1. Click here to Download JQuery.

2. Create one Java Script web resource and upload this downloaded J Query.

  • Go to Settings in Microsoft Dynamics CRM
  • Select Solutions menu from Settings.
  • Open a solution in which you want to trigger a workflow from Solutions.
  • Select Web Resources from Solution.
img

- Add new Resource in Web Resources.

img

Name : Write name of resource,

Display name : Write Display name of resource in web Resource List.

Description : Write Description of resource.

Type : Select type Script (Jscript) from list.

Text Editor : Don not Click on it.

Language : Select Language of resources.

Upload File : Upload JScript file which is downloaded from this.

- Save Resource.

- Publish Resource

3. Create one more JScript web resource for the below code and register to the CRM form where you want to run this script.

- Add new Resource in Web Resources.

Name : Write name of resource,

Display name :Write Display name of resource in web Resource List.

Description : Write Description of resource.

Type : Select type Script (Jscript) from list.

Language : Select Language of resources.

Upload File : Do not upload any file.

Text Editor : Add below Code and Click Ok.

function TriggerWorkflow() { // Get the Workflow ID var workflowId = GetProcessId(); if (workflowId[0].results.length > 0) { var xml = "" + "xml version=\"1.0\" encoding=\"utf-8\"?>" + " <soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> " + GenerateAuthenticationHeader() + " <soap:Body> " + " <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\"> " + " <Request xsi:type=\"ExecuteWorkflowRequest\"> " + " <EntityId>" + Xrm.Page.data.entity.getId() + "</EntityId> " + " <WorkflowId>" + workflowId[0].results[0].WorkflowId + "</WorkflowId> " + " </Request> " + " </Execute> " + " </soap:Body> " + " </soap:Envelope> " + ""; var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", true); xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute"); xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHttpRequest.setRequestHeader("Content-Length", xml.length); xmlHttpRequest.send(xml); } } function GetProcessId() { // Pass the workflow name manually to get the Id of the workflow var processName = "My Workflow Name"; // Get the server URL var serverUrl = Xrm.Page.context.getServerUrl(); var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=Name eq '" + processName + "' and ActiveWorkflowId/Id ne null"; var jSonArray = new Array(); jQuery.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataUri, async: false, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { if (data && data.d != null) { jSonArray.push(data.d); } }, error: function (XmlHttpRequest, textStatus, errorThrown) { alert("Error : has occured during retrieval of the workflowId"); } }); return jSonArray; }

Save Resource.

Publish Resource

- Call the TriggerWorkflow function from the event by which you want to trigger a workflow in Javascript. It can be form load, text change, click events of form or control.

This is the time for Microsoft Dynamics CRM development for practical business solutions at different levels. We are looking forward to keep you informed about more exciting solutions that will be delivered by Microsoft Dynamics CRM platform today and in near future. Just go through the technical approach we have explained in the article and we make you sure you will be able to execute real time workflows using java script in Microsoft Dynamics CRM

For further information, mail us at info@aegissoftwares.com

Read More: