Start a workflow & wait w/ Form

Document created by eharris04 Employee on Apr 26, 2016
Version 1Show Document
  • View in full screen mode

This form and code can help you start a workflow from a form and wait for an update to the form.

 

AuthorVadim Tabakman
Long DescriptionThere are a number of scenarios where could be useful.  Sometimes, you need to perform some complex business process that Nintex Workflow is better suited for.  Kicking off a workflow to do the hard work for you is on way of doing it.  Building a web service that does it and calling that through JS is another option.  But the purpose here it so provide a low-code solution.
Dependencies

JavaScript with the Form

1. Added a Panel to the form.  There is a rule which then hides the panel, if we aren't in Edit Mode.  This is because the JavaScript above will start a workflow on the current item.  There is no Current item in a New item form.

 

 

2. In the panel is a Button (set to JavaScript and it runs a fnStartWorkflow function).  I've given this button a CSS class, so that my JavaScript can disable that button once it's pressed.

 

 

3. In the panel is also a Label, which also has a CSS class associated with it.  The JavaScript uses this label to notify the user of the state of the workflow.

Support InfoVadim Tabakman - http://vadimtabakman.com/nintex-forms-starting-a-workflow-and-waiting.aspx
Compatibility

Nintex Form 2010

Nintex Form 2013

Platform

SharePoint Server 2010

SharePoint Server 2013

Screenshots

Additional Information

Find out more details by visiting Nintex Forms - Starting a Workflow and Waiting - Vadim Tabakman

var varWorkflowStatusColumnInternalName = 'ParentCo';
var varWorkflowName = 'Parent Country Workflow';


function fnStartWorkflow()
{
var soapEnv =
        "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
  <soap:Body> \
    <StartWorkflowOnListItem xmlns='http://nintex.com'> \
      <itemId>ID</itemId> \
      <listName>List Name</listName> \
      <workflowName>" + varWorkflowName + "</workflowName> \
      <associationData></associationData> \
    </StartWorkflowOnListItem> \
  </soap:Body> \
</soap:Envelope>";    
  NWF$.ajax({
        beforeSend: function (request)
        {
          request.setRequestHeader("SOAPAction", "http://nintex.com/StartWorkflowOnListItem");
        },
        url: "Site URL/_vti_bin/NintexWorkflow/Workflow.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResultStartWorkflow,
        contentType: "text/xml; charset=\"utf-8\"",
        error: function (xhr, ajaxOptions, thrownError)
        {
          alert(xhr.status);
          alert(thrownError);
        }
  });
}


var varCheckWorkflowProgressTimer = 0;
var counter = 0;


function processResultStartWorkflow(xData, status) {
    // disable the start workflow button
    NWF$('.cssStartWorkflowButton').attr('disabled', true);
    NWF$('.cssWorkflowStatusLabel').text('Workflow started'); 

    // start a timer to check workflow status every 5 seconds
    varCheckWorkflowProgressTimer = setInterval(fnCheckWorkflowStatus, 5000);
}


function fnCheckWorkflowStatus()
{
var soapEnv =
        "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
  <soap:Body> \
    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
      <listName>List Name</listName> \
      <viewName></viewName> \
      <query> \
       <Query> \
        <Where> \
          <Eq> \
            <FieldRef Name='ID' /> \
            <Value Type='Counter'>ID</Value> \
          </Eq> \
        </Where> \
       </Query> \
      </query> \
      <viewFields> \
       <ViewFields> \
        <FieldRef Name='" + varWorkflowStatusColumnInternalName + "' /> \
       </ViewFields> \
      </viewFields> \
      <rowLimit>1</rowLimit> \
      <queryOptions><QueryOptions/></queryOptions> \
      <webID></webID> \
    </GetListItems> \
  </soap:Body> \
</soap:Envelope>";   
  NWF$.ajax({
        beforeSend: function (request)
         {
          request.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
         },
        url: "Site URL/_vti_bin/Lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResultGetListItems,
        contentType: "text/xml; charset=\"utf-8\"",
        error: function (xhr, ajaxOptions, thrownError)
        {
           alert(xhr.status);
           alert(thrownError);
           if(varCheckWorkflowProgressTimer != 0)
             window.clearInterval(varCheckWorkflowProgressTimer);
         }
  });
}

function processResultGetListItems(xData, status) {
   var varWorkflowStatus = 0;
    NWF$(xData.responseXML).find("z\\:row").each(function() {
        varWorkflowStatus = NWF$(this).attr("ows_ParentCo");
    });


    if(varWorkflowStatus == 2)
    {
      NWF$('.cssWorkflowStatusLabel').text('In progress'); 
    }
    else if(varWorkflowStatus == 5)
    {
      NWF$('.cssWorkflowStatusLabel').text('Workflow completed'); 
      window.clearInterval(varCheckWorkflowProgressTimer);
    }
    else
    {
      NWF$('.cssWorkflowStatusLabel').text('Unknown status'); 
      //window.clearInterval(varCheckWorkflowProgressTimer);
    }
}


 function fnCheckWorkflowProgress() {
    alert('checking...');
    counter++;
    if(counter == 3)
      window.clearInterval(varCheckWorkflowProgressTimer);   
}

Nintex Xchange Terms of Use Policy

Nintex makes no warranty or guarantee about the reliability, performance, quality, or functionality of any assets, and any assets you install are therefore provided as is. By downloading this asset, you agree to the terms of use.

2 people found this helpful

Attachments

Outcomes