1

Am trying to stop multiple workflows around 7-9 workflows on click of a button. Using JSOM to do it with the below code.But am ending up with an error saying Failed to load instances. Can we stop 5+ workflows on a button click?

function terminateWorkflow(listId, itemId) {
var deferred = new $.Deferred();
itemId = parseInt(itemId);
listId = decodeURIComponent(listId);

var context = SP.ClientContext.get_current();
var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
context.load(workflowServicesManager);

var workflowInstanceService = workflowServicesManager.getWorkflowInstanceService();
context.load(workflowInstanceService);
var wfInstances = workflowInstanceService.enumerateInstancesForListItem(listId, itemId);
context.load(wfInstances);

context.executeQueryAsync(
    function(sender, args) {
        var instancesEnum = wfInstances.getEnumerator();
        while (instancesEnum.moveNext()) {
            var instance = instancesEnum.get_current();

            if (instance.get_workflowSubscriptionId().toString() == subId) {
                workflowInstanceService.terminateWorkflow(instance);
                context.executeQueryAsync(

                    function(sender, args) {
                        console.log("Termination Successful");
                        deferred.resolve();
                    },
                    function(sender, args) {
                        console.log("Failed to terminate workflow.");
                        console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());

                    }
                );
            }
        }

    },
    function(sender, args) {
        console.log("Failed to load instances.");
        console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
        deferred.reject(sender, args);
    }
);
return deferred.promise();

 };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.