1

I have what may be a familiar piece of code for starting a SharePoint 2013 workflow, except mine fails with an error of "Error: Value cannot be null. Parameter name: subscription undefined." The log I have to check for this never triggers so I'm not sure why the value suddenly isn't being passed properly.

<a href="#" onclick="startWorkflow('2', '947ABD00-0EB4-454D-B561-89CBF6205581')">New Quick Start on 2</a>//using specific item ID to test

<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.workflowservices.js"></script>

<script type="text/javascript">
function startWorkflow(itemID, subID) {

var context = SP.ClientContext.get_current();
var web = context.get_web();

var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);

var subService = wfServiceManager.getWorkflowSubscriptionService()

context.load(subService);

context.executeQueryAsync(
    function(sender, args){
        var subscription = subService.getSubscription(subID);

if(subscription == null){console.log("Null Subscription")}

        if(subscription){

        console.log("Subscription load success. Attempting to start workflow.");        
        var inputParameters = {};

        wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemID, new Object());
        }

        context.executeQueryAsync(
            function(sender, args){ console.log("Successfully starting workflow."); },
            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());
    }
);
}
</script>

2 Answers 2

1

Alas everytime you publish a Workflow it gets a new (GU)ID,

So starting by Workflow ID is a pain

You can start by Name (Note, this came from a button in a View, I deleted lines before pasting, so there might be typos)

event.preventDefault();
var TR = GetAncestor(this, 'TR');
var itemID = TR.id.split(',')[1];
var wfName = 'YOUR_WORKFLOW_TITLE_GOES_HERE';
SP.SOD.executeFunc('sp.workflowservices.js', 'SP.WorkflowServices.WorkflowServicesManager',
    function () {
        var ctx = new SP.ClientContext.get_current(),
            wfsManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web()),
            wfSubs = wfsManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList(_spPageContextInfo.pageListId);
        ctx.load(wfSubs);
        ctx.executeQueryAsync(function () {
            wfsEnum = wfSubs.getEnumerator();
            while (wfsEnum.moveNext()) {
                var wfSub = wfsEnum.get_current();
                if (wfSub.get_name() === wfName) {
                    wfsManager.getWorkflowInstanceService().startWorkflowOnListItem(wfSub, itemID, new Object());
                    SP.UI.Notify.addNotification('Init Workflow: ' + wfName + ' on item: ' + itemID, false);
                }
            }
        },function(e){console.error(e)});
    });
0

Building on the solutions provided by others, here's the code I was able to use. NOTE: You need to have your workflow set to "Allow this workflow to be started manually."

/**
*   Starts a SharePoint 2013 Workflow on a particular list item.
*   Params:
*       workflowName: The name of the Workflow
*       listGUID: the GUID of the list
*       itemId: the ID of the list item
*/  
function startListWorkflow(workflowName, listGUID, itemId){
    SP.SOD.executeFunc("sp.js", "SP.ClientContext" , function(){
        SP.SOD.registerSod('sp.workflowservices.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.workflowservices.js'));
        SP.SOD.executeFunc('sp.workflowservices.js', "SP.WorkflowServices.WorkflowServicesManager", 
            function(){
                var ctx = new SP.ClientContext.get_current(),
                wfsManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web()),
                wfSubs = wfsManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList(listGUID);
                ctx.load(wfSubs);
                ctx.executeQueryAsync(function () {
                    wfsEnum = wfSubs.getEnumerator();
                    while (wfsEnum.moveNext()) {
                        var wfSub = wfsEnum.get_current();
                        if (wfSub.get_name() === workflowName) {
                            var initiationParams = {}; 
                            wfsManager.getWorkflowInstanceService().startWorkflowOnListItem(wfSub, itemId, initiationParams);
                            ctx.executeQueryAsync(
                               function (sender, args) {
                                    console.log("Successfully started workflow.");
                               },
                               function (sender, args) {
                                    console.log("Failed to start the workflow.");
                                    console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
                               }
                           );
                        }
                    }
                },function(e){
                    console.error(e)
                });
            }
        );
    });         
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.