Running order guides from scripts

Running order guides with a server-side script is more complex than using workflows, but it allows more flexibility and can be used in non-workflow situations.

For example, you can use order guide scripts with UI actions or server-side business rules.
Note: When order guides run automatically, order guide UI policies are not enforced. Also, options in the Choose Options screen cannot be selected, so make sure that order guide rules define sensible defaults for these options to avoid processing failures.

Use the SNC.ScriptableOrderGuide Java class to run order guides with a script.

Use the SNC.ScriptableOrderGuide(String orderGuideId) constructor to create a new ScriptableOrderGuide object.

Method Summary

Table 1. Method Summary
Method Return Value Description
process(String json) boolean Runs the order guide using the JSON encoded string parameter as the input for the the order guide. Returns true or false depending on whether processing was successful or not.
Note: Both opened_by and requested_for parameters must be passed to the order guide, and both must have valid user record sys_id values.

If processing is successful and a request is created by the order guide, you can retrieve the request GlideRecord using getRequest.

If the processing fails, you can retrieve the failure GlideRecord using getFailure(), then submit the script for reprocessing using reprocess.

reprocess(GlideRecord failure) boolean Runs the order guide again using the JSON encoded string parameter stored in the failure GlideRecord.
getMessage() String Retrieves the message populated after processing or reprocessing.
getRequest() GlideRecord Retrieves the request GlideRecord.
getFailure() GlideRecord Retrieves the failure GlideRecord from the Scriptable Order Guide Failures [sc_script_order_guide_failure] table.

Script Example

This script processes an order guide called IT Onboarding SOG.
// Creating the object to later be JSON encoded 
var json = {"opened_by":"62826bf03710200044e0bfc8bcbe5df1","requested_for":"06826bf03710200044e0bfc8bcbe5d8a","department":"221f3db5c6112284009f4becd3039cc9"};
var gr = new GlideRecord("sc_cat_item_guide");
if (gr.get("name","IT Onboarding SOG")) {
  var sog = new SNC.ScriptableOrderGuide(gr.getValue("sys_id"));
  var result = sog.process(new JSON().encode(json));
    gs.log("Processing the scriptable order guide failed with message: " + sog.getMessage());
  else { 
    var request = sog.getRequest();
    gs.log("Request created - " + request.sys_id); } }