cancel
Showing results for 
Search instead for 
Did you mean: 
Workflow Hero

Start Nintex Workflow with Button on site page using ECMA script

I able  to implement a successful solution using SharePoint Designer workflows. however, when I try to trigger the Nintex workflow by clicking on a button is failing with below error.

Error: Value cannot be null.

Parameter name: subscription undefined

Below is the code snippet I used with SPD workflow and the same I tried to trigger Nintex workflow:

var WorkflowInstanceID = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx";

var web = clientContext.get_web();

var wfServiceManager = new SP.WorkflowServices.WorkflowServicesManager(clientContext, web);

var subscription = wfServiceManager.getWorkflowSubscriptionService().getSubscription(WorkflowInstanceID);

clientContext.load(subscription);

clientContext.executeQueryAsync(

function(sender, args)

{

var inputParameters = {};

wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, inputParameters);

clientContext.executeQueryAsync(

function(sender, args)

{

alert("Email Sent");

},

function(sender, args)

{

alert("Email Failed to Sent \n Error: " + args.get_message() + "\n" + args.get_stackTrace());

}

);

},

function(sender,args)

{

alert("Email Failed to Sent \n Error: " + args.get_message() + "\n" + args.get_stackTrace());

});

Appreciate your quick response

Labels: (2)
0 Kudos
Reply
7 Replies
Workflow Hero

Re: Start Nintex Workflow with Button on site page using ECMA script

Please check the few options listed here Re: Start workflow with a button

If any work as you need, please reply with your best option. Or if you have any questions, let us know.

Accept as Solution Reply
Workflow Hero

Re: Start Nintex Workflow with Button on site page using ECMA script

No Andrew, it doesn't help. My script is failing at the below line

var subscription = wfServiceManager.getWorkflowSubscriptionService().getSubscription(WorkflowInstanceID);

Basically the WorkflowInstanceID looks incorrect. I am using the parameter from the below URL

http://sp2013demo.softura.com/sites/dawnfoods/_layouts/15/NintexWorkflow/WorkflowDesigner.aspx?Workf...

0 Kudos
Accept as Solution Reply
Workflow Hero

Re: Start Nintex Workflow with Button on site page using ECMA script

Could you use a button that navigates to the url that is similar to

Site URL/_layouts/15/NintexWorkflow/StartWorkflow.aspx?List={List ID}&ID=ID&WorkflowName=MyWorkflow&Source=Item URL.

Accept as Solution Reply
Workflow Hero

Re: Start Nintex Workflow with Button on site page using ECMA script

Kalyan, I think the issue has to do with how you're getting the workflow template ID. In my research for this same topic, I came across a method that we use successfully to start a workflow on multiple selected items in a list. IT uses an SPServices() function. Marc Anderson wrote the script, and in it he uses the workflow name to get the specific GUID. That GUID, along with the current item's URL and any workflow parameters is used to start the workflow. Here's an example of the code, but I suggest you read through his example script for the details.

      //Before starting the workflow, we use GetTemplatesForItem to get Workflow Template Id.

      $().SPServices({

        operation: "GetTemplatesForItem",

        item: itemURL,

        async: false,

        completefunc: function (xData,Status) {

          var currentItemURL = this.item;

          $(xData.responseXML).find("WorkflowTemplates > WorkflowTemplate").each(function (i,e) {

            // SET THE WORKFLOW NAME HERE!

             if ($(this).attr("Name") == "Create RecordLabel") {

              var guid = $(this).find("WorkflowTemplateIdSet").attr("TemplateId");

              if (guid != null) {

                workflowGUID = "{" + guid + "}";

                $().SPServices({

                  operation: "StartWorkflow",

                  item: currentItemURL,

                  templateId: workflowGUID,

                  workflowParameters: workflowParams,

                  async: true,

                  completefunc: function () {

                    if (total == counter) {

                      if (waitDialog != null) {

                        waitDialog.close();

                      }

                      SP.UI.Notify.addNotification("Started workflow process for selected item.",false);

                      window.location.reload();

                    }

                    counter++;

                  }

                });

              }

            }

          });

        }

      });

Hope this gets you further along.

Gerard

Accept as Solution Reply
Workflow Hero

Re: Start Nintex Workflow with Button on site page using ECMA script

Hello Gerard,

I am glad that you are looking at my issue.

When I try to run the code the fetch the Workflow ID, I am getting the below error.

Code I tried to execute,

<script src="SiteAssets/Scripts/jquery-1.11.3.js"></script>

<script src="SiteAssets/Scripts/jquery.SPServices-2014.02.min.js"></script>

<script type="text/ecmascript">

var collListItem;

var clientContext;

ExecuteOrDelayUntilScriptLoaded(GetWorkflowID(), 'jquery.SPServices-2014.02.min.js');

function GetWorkflowID()

{

  WorkflowInstanceID = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx";

  var workflowGUID = null;

  $().SPServices({

  operation: "GetTemplatesForItem",

  item: "/Lists/SampleList/DispForm.aspx?ID=390",

  async: true,

  completefunc: function (xData, Status) {

  alert(Status);

     $(xData.responseXML).find("WorkflowTemplates > WorkflowTemplate").each(function(i,e) {

  // hard coded workflow name

  if ( $(this).attr("Name") == "Workflow Name" ) {             

  var guid = $(this).find("WorkflowTemplateIdSet").attr("TemplateId");       

  if ( guid != null )

  {

  workflowGUID = "{" + guid + "}";

  }

  }

  });

  }

  });

}

</script> ​​​​

Error I am getting

POST http://sp2013demo.testdomain.com/sites/testsite/_vti_bin/Workflow.asmx 500 (Internal Server Error)

I able to access the Workflow.asmx  without any issue with the same user ID.

Not sure what am I missing.

0 Kudos
Accept as Solution Reply
Workflow Hero

Re: Start Nintex Workflow with Button on site page using ECMA script

Kalyan, I think the issue has to do with you're attempting to run the GetWorkflowID function directly and without the clientContext. You make references to "$(this)" - which is referring to the clientContext defined in the calling routine. I think your attempt to leave out the multi-select portion removed too much.

My experience with this is that you really need two discreet functions. The main function called by the custom action. In this function you create the clientContext and use it to query the items. You could modify the query to only select the one whose ID matches what you expect. This way you still have a context on which to run the "executeQueryAsync" - which calls your GetWorkflowID function.

In your workflowID function be sure to set the workflow parameters variable as shown:

var workflowParams = "<root />";

and include it in SPServices function. The function won't operate without it:

$().SPServices({

   operation: "StartWorkflow",

   item: currentItemURL,

   templateId: workflowGUID,

   workflowParameters: workflowParams,

Good luck,

Gerard

Accept as Solution Reply
Workflow Hero

Re: Start Nintex Workflow with Button on site page using ECMA script

‌ did you resolve this?

Accept as Solution Reply