Wednesday, 27 November 2024

CRM 365 Online - TypeScript - Execute Workflow


Please find a snippet code below for executing a workflow from TypeScript.

The only requirement here is to set the WorkflowId and the Id of the record plus the errorMessage and successMessage to use.

Xrm.Utility.showProgressIndicator("Creating .......");



let request =

{

    entity: { id: workflowID, entityType: "workflow" },

    EntityId: { guid: Id },

    getMetadata: function () {

        return {

            boundParameter: "entity",

            operationType: 0,

            //operation name is always “Execute Workflow” independent of workflow name

            operationName: "ExecuteWorkflow", parameterTypes: {

                "entity": {

                    "typeName": "mscrm.workflow",

                    "structuralProperty": 5

                },

                "EntityId": {

                    "typeName": "Edm.Guid",

                    "structuralProperty": 1

                }

            }

        }

    }

};



Xrm.WebApi.online.execute(request).then(

    function (result: any) {

        //alert("The confirmation email will be generated.");

        debugger;


        Xrm.Utility.closeProgressIndicator();


        let alertStrings = { confirmButtonLabel: "Close", text: successMessage, title: "Success" };

        let alertOptions = { height: 200, width: 400 };

        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(

            function (success) {

                console.log("Alert dialog closed");

            },

            function (error) {

                console.log(error.message);

            }

        );

    },

    function (error: any) {


        debugger;


        Xrm.Utility.closeProgressIndicator();


        let alertStrings = { confirmButtonLabel: "Close", text: errorMessage, title: "Error" };

        let alertOptions = { height: 200, width: 400 };

        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(

            function (success) {

                console.log("Alert dialog closed");

            },

            function (error) {

                console.log(error.message);

            }

        );


    });


Hope it helps.

No comments:

Post a Comment

CRM 365 Cloud - Disassociate 2 records using typescript

In case you need to Disassociate 2 records, please find below the code that allows you to do that.      export async function DissociateE...