Execute on-demand workflow using JavaScript in D365

1. Create a on-demand workflow in MS CRM
2. Retrieve the workflow GUID(http://XXX/xx/XRMServices/2011/OrganizationData.svc/WorkflowSet?$filter=Name eq 'Test Workflow'&$select=WorkflowId
ActiveWorkflowId/Id ne null)
3. Execute the wokrflow
4.        executeOnDemandWorkflow: function (recordId, workflowId) {
            var responseResult;
            try {
                var clientUrl = VeriLoan.XrmHelper.getOrganizationURL();
                var query = "workflows(" + workflowId.replace("}", "").replace("{", "") + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
                //setup data object passing in record id
                var data = {
                    "EntityId": recordId,
                }

                var req = new XMLHttpRequest();
                req.open("POST", clientUrl + "/api/data/v8.2/" + query, false);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.setRequestHeader("OData-MaxVersion", "4.0");
                req.setRequestHeader("OData-Version", "4.0");

                req.onreadystatechange = function () {
                    if (this.readyState == 4) {
                        req.onreadystatechange = null;
                        if (this.status == 200 || this.status == 204) {
                            console.log("Success");
                            responseResult = this.response;
                        } else {
                            responseResult = this.response != null ? JSON.parse(this.response).error : this.response;
                        }
                    }
                };
                req.send(JSON.stringify(data));

            } catch (e) {
                alert("failedtoinvokeWorkflow", e);
            }

            return responseResult;
        }

Comments

Popular posts from this blog

Display SSRS Report in CRM Dashboard with Paging and Hyperlink