CRM Crate

How to call a workflow from JavaScript?

We can easily call a workflow from a JavaScript code. For this we will need the workflow GUID and the GUID of the record from which you will trigger the workflow.
Note : – Click the checkbox “Child Process” in order to call the workflow externally via JavaScript.

Below code demonstrates how to call the workflow from JavaScript.

        var formContext = primaryControl.getFormContext();    
        var entityId = formContext.data.entity.getId();
        var workflowId = "82CE4F9C-F9DC-482E-1F08-12CD969410C4";
        var query = "";
        try {
            //Define the query to execute the worklfow.
            query = "workflows(" + workflowId.replace("}", "").replace("{", 
             "") + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
            var data = {
                "EntityId": entityId
            };
            //Create a request                
            var req = new XMLHttpRequest();
            req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + 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 /* complete */) {
                    req.onreadystatechange = null;

                    if (this.status == 200) {
                        //success callback this returns null since no return value available.
                        var result = JSON.parse(this.response);


                    } else {
                        //error callback
                        var error = JSON.parse(this.response).error;
                    }
                }
            };
            req.send(JSON.stringify(data));
        } catch (e) {
            alert(e)
        }

Click here for understanding more about workflows.

CRM Crate

All In One Platform For Learning Microsoft CRM.

Facebook
Twitter
LinkedIn
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.