I have a function to start a workflow on multiple checked list items. It successfully identifies which items are selected and successfully starts the workflow, however it is only doing it on the last selected item.
The for loop starts the workflow multiple times on the last item rather than once on each item.
Could somebody please tell me what I'm doing wrong?
function startWorkflow() {
var subID = "A2075514-B351-4A63-8511-626A2519AAD4";
var context = SP.ClientContext.get_current();
var web = context.get_web();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
var subscription = wfServiceManager.getWorkflowSubscriptionService().getSubscription(subID);
for (item in selectedItems)
{
var itemId = selectedItems[item].id;
console.log(itemId);
context.load(subscription);
context.executeQueryAsync(
function(sender, args){
console.log("Subscription load success. Attempting to start workflow on " + itemId + ".");
var inputParameters = {};
wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, inputParameters);
context.executeQueryAsync(
function(sender, args){ console.log("Successfully starting workflow on " + itemId + "."); },
function(sender, args){
console.log("Failed to start workflow.");
console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
}
);
},
function(sender,args){
console.log("Failed to load subscription.");
console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
}
);
}
}