I'm attempting to write a script that will cancel and start workflows when an error happens. I found this section of code on different sites but have not been able to get it to work. After stepping through the code I know the instancesEnum is empty. If it makes a difference, I'm working with SharePoint 2013 with a SharePoint 2010 workflow.
function test(){
var title; var workflow; var modified;
var listID = "672AF4F1-7160-4D93-B83B-2DA5C0F298B1";
var workflowID = "49DB5E25-E862-49DC-BE90-683B2AC9C6F8";
$().SPServices({
operation: "GetListItems",
listName: "My Testing",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
title = $(this).attr("ows_Title");
workflow = $(this).attr("ows_TestCrea");
modified = $(this).attr("ows_Modified");
id = parseInt($(this).attr("ows_ID"));
if(workflow == 2){ //Using in progress instead of error for testing
console.dir(title + " -" + workflow + " -" + modified + " " + id);
terminateWorkflow(listID,id ,workflowID);
}
});
}
});
}
function terminateWorkflow(listId, itemId, subId){
var context = SP.ClientContext.get_current();
var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
var workflowInstanceService = workflowServicesManager.getWorkflowInstanceService();
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");
},
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());
}
);
};