Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Javascript run a Custom Workflow and handle Stop Workflow Canceled

Posted on by 182

I am trying to figure out a way in Javascript how to differentiate a Custom Workflow execution that terminates successfully from one that has been terminated with the step Stop workflow with status of: Canceled.

The goal is trivial: communicating the user that the execution has been canceled for a valid reason providing a message

I have defined a simple WorkFlow as per image to be executed in background by a click on a ribbon:

pastedimage1588066824479v2.png

a step of the WF states that if the field of the main entity WorkOrder called ReportedByContact is empty then: Stop workflow with status of: Canceled and set the Status Message with "MyErrorMessage"

In javascript I managed to execute the WorkFlow but even if it terminates in canceled the Status of the response is 200 and furthermore I am not able to get "MyErrorMessage".

Check the code here:

function (entityId, workflowName) {    
    //get the workflow id by name
    Xrm.WebApi.retrieveMultipleRecords("workflow", "?$select=workflowid&$filter=name eq '"   workflowName   "' and statecode eq 1").then(
        function success(results) {
            
            for (var i = 0; i < results.entities.length; i  ) {
                workflowId = results.entities[i]["workflowid"];
            }

            try {

                //Define the query to execute the action
                var 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//Entity Type
                                },
                                "EntityId": {
                                    "typeName": "Edm.Guid",
                                    "structuralProperty":1//Primitive type
                                }
                            },
                            operationType: 0,
                            operationName:"ExecuteWorkflow",
                        }
                    }
                }
                var triggerCustomWFRequest = new ExecuteWorkFlow(entityId, workflowId);
                Xrm.WebApi.online.execute(triggerCustomWFRequest).then(
                    function successCallback(result) {
                        //workflow terminates successfully even if a step canceled its execution
                        var IWouldLikeToKnow_MyCustomMessage = ????//
                        var IWouldLikeToKnowThatTheWF_HasBeenCanceled = ???//
                        if (result.ok) {
                            //result.ok is true even if the workflow execution has been canceled
                            alert("ok");
                        }
                    },
                    function errorCallback(result) {
                        alert(result.message);
                    }
                );
            } catch (e) {
                //...
            }

        },
        function (error) {
            //...
        }
    );
};

How can I understand in javascript the workflow has been canceled?

How can I get in javascript the Status Message "MyErrorMessage"?

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript run a Custom Workflow and handle Stop Workflow Canceled

    If your workflow is background you will have to write additional waiting logic that will wait till the end of the execution of the workflow, analyze the result and show it. Not so fancy but doable. The last time I was doing it was 4.0 times so code is not relevant now.

  • ___Pool___ Profile Picture
    ___Pool___ 182 on at
    RE: Javascript run a Custom Workflow and handle Stop Workflow Canceled

    Hi Andrew, Real Time Workflows when canceled execute the error callback and your code works. My workflow being a Background Process at the end executes the success callback even if the WF is being canceled

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript run a Custom Workflow and handle Stop Workflow Canceled

    Hello,

    If workflow was stopped with "Cancel" reason your code will land in errroCallback. To handle that situation you can use following code:

    function (error) {

               Xrm.Navigation.openErrorDialog({

                   message: "Workflow execution failed" + (error.message ? " - " + error.message : ""),

                   errorCode: error.errorCode,

                   details: (error.innerror && error.innerror.message) ? error.innerror.message : error.message

               });

           }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans