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:
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"?