MSCRM :: Execute Workflow from Javascript

MSCRM :: Execute Workflow from Javascript

After reading a few blog, and retyping screen show please see below.

Orginal Blog Click Here

 

//JavaScript source code
//Created: 21/06/2020
//Updated: 30/06/2020
//Developed By: Gareth Cheyne, Harvey Norman NZ
//eMail: gareth.cheyne@nz.harveynorman.com

// Define HN.OpportunityProduct namespace
var HN = HN || {};

HN.OpportunityProduct = {
    cloneProduct: function (ids) {

        // Open the progress modal
        Xrm.Utility.showProgressIndicator("Cloning Products, Please Wait...")

        // The workflow for this entity type
        var workflowId = "E63168E3-F643-40FF-B8BC-64C7645CFF0F"; 

        // Iterate over the selected values
        for (var i = 0; i < ids.length; i++){

            // Execute the workflow
            var triggerWorkflow = new HN.Tools.executeWorkflow(ids[i], workflowId);
            Xrm.WebApi.online.execute(triggerWorkflow).then(
                function success(result) {
                    if (i == ids.length) {
                        // Refresh the subgrib, the parameter name must be passed.
                        Xrm.Page.ui.controls.get("opportunity_subgrid").refresh()

                        // Close the progress modal
                        Xrm.Utility.closeProgressIndicator()
                    }
                },
                function (error) {
                    console.log(error);
                    if (i == ids.length) {
                        // Refresh the subgrib, the parameter name must be passed.
                        Xrm.Page.ui.controls.get("opportunity_subgrid").refresh()

                        // Close the progress modal
                        Xrm.Utility.closeProgressIndicator()
                    }
                }
            )
        }
    },
}

HN.Tools = {

    // Allow the execution of a work flow via Xrm.WebApi.
    executeWorkflow: function (entityId, workflowId) {
        this.EntityId = { "guid": entityId };
        this.entity = { id: workflowId, entityType: "workflow" };
        this.getMetadata = function () {
            return {
                boundParameter: "entity",
                parameterTypes: {
                    "entity": {
                        "typeName": "Microsoft.Dynamics.CRM.workflow",
                        "structuralProperty": 5
                    },
                    "EntityId": {
                        "typeName": "Edm.Guid",
                        "structuralProperty": 1
                    }
                },
                operationType: 0,
                operationName: "ExecuteWorkflow",
            };
        };
    }
}


The above is running a unique workflow that I have to Clone a Opportunity Product with help from Dynamics-365-Workflow-Tools