Wednesday, May 19, 2021

Execute Dynamics 365 Workflow Using JavaScript on Click of Ribbon Button

 👉Trigger Dynamics 365 Workflow Using JavaScript On Click Of Ribbon Button

In Dynamics 365, you might come across a certain scenario where you want to execute a workflow using javascript code, and the same you may require to execute on ribbon button click.

This can be achieved through WebAPI calls.

Let’s start, 
We have an on-demand workflow that is created on Email activity and there is a ribbon button placed on the same email activity on the click of this button we set some of the field value on the email form as well as on the related entity records form.

1. Create an on-demand workflow and save and activate, as shown in the image below:



2. Get the workflow ID,  hardcode the Workflow Id, and Email Id (You can make the WebAPI call and get ID workflow ID and Email ID).  

Copy the Workflow Id from the URL: 


3.  Add email activity in a solution and open the solution from the ribbon workbench and add the button on the email form. Follow my previous blog to add a button on the ribbon - > Click here to add a button on the ribbon 


4 . Write the JavaScript Code and call it on the ribbon button  See how to call Javascript from the ribbon

Calling from ribbon workbench:



OnClickRibbonButton: function (primaryControl) {
        try {
            var formContext = primaryControl;
            var currentRecordGUID = formContext.data.entity.getId().replace(/[\])}[{(]/g, '');
            var workflowGUID = "e58deac6-fcdf-485a-a517-1ca020dcc86f";
            ExecuteWorkflow(formContext, workflowGUID, currentRecordGUID);
        }
        catch (error) { }

    },


5. Write a method to execute the workflow and call the below method on the button click method:

ExecuteWorkflow: function (context, workflowId, recordId) {
        var formContext = typeof context.getFormContext === "function" ? context.getFormContext() : context;
        var data = {
            "EntityId": recordId
        };
        var WorkflowId = workflowId;
        var req = new XMLHttpRequest();
        req.open("POST", formContext.context.getClientUrl() + "/api/data/v9.1/workflows(" + WorkflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200 || this.status === 204) { // Asynchronous || Synchronous
                    formContext.ui.refreshRibbon(); 
                }
                else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(data));
    },


Click here to learn more: Javascript Common Function    Home
-------------------------------------------------------------------------------------------

Thanks !!!
Happy Leaning ...
Keep CRMing....

No comments:

Post a Comment