Saturday, September 15, 2018

Run any workflow by utilizing Scheduled Scripts and Script Parameters


1.      Setup the Workflow
2.      Create a Scheduled script using the code snippet below: 
 
function startScheduledWorkflow()
{
      var savedSearch = nlapiGetContext().getSetting('SCRIPT', 'custscript_saved_search_to_use');
      var wfmWorkflow = nlapiGetContext().getSetting('SCRIPT', 'custscript_workflow_to_execute');
      var recordType = nlapiGetContext().getSetting('SCRIPT', 'custscript_record_type');

      var searchResults = nlapiSearchRecord(recordType, savedSearch, null, null);

      for ( var i = 0; searchResults != null && i < searchResults.length; i++ )
      {
            var searchResult = searchResults[i];
            nlapiInitiateWorkflow(recordType, searchResult.getId(), wfmWorkflow);
      }
}
 


3.      Add three (3) new script parameters to point to the saved search to use, the Workflow to run and the record type. See below: 
 

Image 
 

4. Deploy the scheduled script on user's desired schedule.
5. On the Parameters tab, choose the saved search to where user would want the workflow to execute.
6. Also, choose the appropriate workflow to use and the record type where it is applied.



Image

7. Hit on Save & Run to perform the on-demand Workflow, or just run it on a schedule. See Solution 14577 for more information about running scheduled scripts.


ADDITIONAL NOTES:

- In reality, the user may choose mismatching values for the parameters. For example, the saved search applies to a Customer record type and the chosen record type is Transaction. This will fail. It may require additional validation on the script.
- This can be used to schedule any workflow created
- This is a reusable script. Just add a new script deployment for each workflow that user want to schedule.

No comments:

Post a Comment