hits counter
Google
Pinterest
Linkedin
YouTube
Skype

Trigger Workflow Using Javascript Button Click Event

Quick Inquiry

Client Speaks

Client Speaks - Testimonials for Aegis Software


You Are @ >> Home >> Articles >> Trigger Workflow Using Javascript Button Click Event

Today I explain how to trigger CRM workflow from a java script function.

For this example, I created a Workflow InMicrosoft Dynamics CRM which will just update theName of a Contact. The name of workflow is “Update Contact Name”. This name will use to retrieve workflowID.

Create one html page with one textbox, one button control and JQ-Grid control. Include required js and css for jqgrid like following in header section.

<link rel="stylesheet" type="text/css" href="Jquery.ui.css"> <link rel="stylesheet" type="text/css" href="ui.jqgrid.css"> <script type="text/javascript" src="Js/jquery.js"></script> <script type="text/javascript" src="Js/jquery_ui_custom.min.js"></script> <script type="text/javascript" src="Js/jquery.jqGrid.js"></script>

Then create one web-resource in CRM for this html page. In this html page I have created a function with retrieve all contact records and display in JQ-Grid. This JQ-Grid has facility to select records by checkbox. These selected records will be updated on button click event.

First we require aworkflowID, the following code will retrieve workflowID by workflow name. In this page I have one global variable to store workflowID.Get the workflow name from textbox which passed by user and pass into function or you can pass hardcode workflow name which you want to execute. In my example workflow name is “Update Contact Name” that I passed directly.

Call UpdateRecord function form button click event which contains getworkflowID and ExecuteWorkflow functions like this.

//write this code in document.ready var btn2 = $("#btnGenerate"); btn2.bind('click', function (event) { UpdateRecord(); }); //main function to call from button click event; functionUpdateRecord() { getWorkflowId('Update Contact Name'); var Ids = jQuery("#contact").jqGrid('getGridParam', 'selarrrow'); //this is used to retrieve all selected records from jqgrid; //following lines of code execute workflow for each selected row. if (Ids.length> 0) { for (vari = 0; i< Ids.length; i++) { ExecuteWorkflow(Ids[i], workFlowId); } } } //this function will called form UpdateRecord function; functiongetWorkflowId(workflowName) { varodataSelect = Xrm.Page.context.getClientUrl() + '/XRMServices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + workflowName + '\''; varreq = newXMLHttpRequest(); req.open("GET", encodeURI(odataSelect), false); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; //avoids memory leaks if (this.status == 200) { workFlowId = (JSON.parse(this.responseText).d).results[0].WorkflowId; } else { errorCallback(JSON.parse(this.responseText)); } } }; req.send(); } //this function will called form UpdateRecord function; functionExecuteWorkflow(EntityIdValue, WorkflowIdValue) { // Creating the request XML for calling the Action varurl = Xrm.Page.context.getServerUrl(); varOrgServicePath = "/XRMServices/2011/Organization.svc/web"; url = url + OrgServicePath; var request; request = "<s:Envelopexmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<s:Body>" + "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">" + "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">" + "<a:KeyValuePairOfstringanyType>" + "<c:key>EntityId</c:key>" + "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + EntityIdValue + "</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/\">" + WorkflowIdValue + "</c:value>" + "</a:KeyValuePairOfstringanyType>" + "</a:Parameters>" + "<a:RequestId i:nil=\"true\" />" + "<a:RequestName>ExecuteWorkflow</a:RequestName>" + "</request>" + "</Execute>" + "</s:Body>" + "</s:Envelope>"; varreq = newXMLHttpRequest(); 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"); req.onreadystatechange = function () { alert(“Record Updated”); }; req.send(request); }

So at the end of the whole process, you will see several system jobs for “Update Contact’s name” workflow. The system jobs will be for all contacts which you selected from the grid to update name. If you see all contacts were updated then you succeed in your mission.


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