{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":602,"path":"api-reference/scripts/c_RunningOrderGuidesAutomatically.md","title":"Running order guides automatically","bundle":null,"doc_type":null,"release":"australia","body":"\n# Running order guides automatically\n\nService catalog order guides allow customers to make a single service catalog request that can generate several ordered items. Administrators can configure order guides to run automatically, from a workflow or a script to generate a set of ordered items without manually submitting a service catalog request. Administrators can also review and reprocess the order guide failures.\n\nAs a use case, an onboarding workflow for a new employee can run an order guide to automatically order items for that employee.\n\n**Note:** You can only save catalog items, not the order guide \\(that is, initial landing page options\\).\n\n**Parent Topic:**[Server-side scripting](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_ServerScripting.md)\n\n## Running order guides from scripts\n\nRunning 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.\n\nFor example, you can use order guide scripts with UI actions or server-side business rules.\n\n**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.\n\nUse the SNC.ScriptableOrderGuide Java class to run order guides with a script.\n\nUse the SNC.ScriptableOrderGuide\\(String orderGuideId\\) constructor to create a new ScriptableOrderGuide object.\n\n### Method summary\n\n<table id=\"table_uzq_cdf_mp\"><thead><tr><th>\n\nMethod\n\n</th><th>\n\nReturn Value\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nprocess\\(String json\\)\n\n</td><td>\n\nboolean\n\n</td><td>\n\nRuns the order guide using the JSON encoded string parameter as the input for 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.\n\n If processing is successful and a request is created by the order guide, you can retrieve the request GlideRecord using getRequest\\(\\).\n\n If the processing fails, you can retrieve the failure GlideRecord using getFailure\\(\\), then submit the script for reprocessing using reprocess.\n\n</td></tr><tr><td>\n\nreprocess\\(GlideRecord failure\\)\n\n</td><td>\n\nboolean\n\n</td><td>\n\nRuns the order guide again using the JSON encoded string parameter stored in the failure GlideRecord.\n\n</td></tr><tr><td>\n\ngetMessage\\(\\)\n\n</td><td>\n\nString\n\n</td><td>\n\nRetrieves the message populated after processing or reprocessing.\n\n</td></tr><tr><td>\n\ngetRequest\\(\\)\n\n</td><td>\n\nGlideRecord\n\n</td><td>\n\nRetrieves the request GlideRecord.\n\n</td></tr><tr><td>\n\ngetFailure\\(\\)\n\n</td><td>\n\nGlideRecord\n\n</td><td>\n\nRetrieves the failure GlideRecord from the Scriptable Order Guide Failures \\[sc\\_script\\_order\\_guide\\_failure\\] table.\n\n</td></tr></tbody>\n</table>### Script example\n\nThis script processes an order guide called IT Onboarding SOG.\n\n```\n// Creating the object to later be JSON encoded \nvar json = {\"opened_by\":\"62826bf03710200044e0bfc8bcbe5df1\",\"requested_for\":\"06826bf03710200044e0bfc8bcbe5d8a\",\"department\":\"221f3db5c6112284009f4becd3039cc9\"};\n \nvar now_GR = new GlideRecord(\"sc_cat_item_guide\");\nif (gr.get(\"name\",\"IT Onboarding SOG\")) {\n  var sog = new SNC.ScriptableOrderGuide(gr.getValue(\"sys_id\"));\n  var result = sog.process(new JSON().encode(json));\n  if(!result)\n    gs.log(\"Processing the scriptable order guide failed with message: \" + sog.getMessage());\n  else { \n    var request = sog.getRequest();\n    gs.log(\"Request created - \" + request.sys_id); } }\n```\n\n## Running order guides from workflows\n\nRunning an order guide from a workflow is suitable if you include order guides as part of a broader workflow-based process.\n\nFor example, an activity within an onboarding workflow for a new employee can automatically run an order guide to order items for that employee.\n\n**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.\n\nTo run order guides from a workflow, use the **Scriptable Order Guide** workflow activity.\n\n<table id=\"table_kcr_hbf_mp\"><thead><tr><th>\n\nField\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nOrder Guide\n\n</td><td>\n\nThe name of the order guide that this activity processes. For example, **Example Employee Onboarding IT**.\n\n</td></tr><tr><td>\n\nScript\n\n</td><td>\n\nA script passing information to the order guide. This information is sent as a JSON encoded string parameter assigned to the *answer* variable.The script must meet these requirements:\n\n-   The names of the variables in the script must match the names used within the order guide. For example, if the order guide uses a *department* variable in a rule condition, the script must also pass a **department** parameter.\n-   Both **opened\\_by** and **requested\\_for** parameters must be passed to the order guide, and both must have valid user record sys\\_id values.\n\n</td></tr></tbody>\n</table>### Results\n\n-   **Success**: the activity successfully processed the order guide. This does not mean a request was created. If a request was created, the request sys\\_id is added to the workflow scratchpad under the *sc\\_request* variable.\n-   **Failure**: while processing the order guide a failure occurred, creating a failure record. If the processing fails, you can view and edit the failure record.\n\n### Workflow example\n\nThe **Example Employee Onboarding IT Workflow** workflow uses this example to generate IT catalog items for a new employee as part of an onboarding process.\n\nThe activity uses this script to:\n\n1.  Take a JSON string generated previously from the HR change record.\n2.  Append the mandatory **opened\\_by** and **requested\\_for** parameters to that string.\n3.  Submit the new string for processing by the order guide.\n\n```\nvar parameters  = new JSON().decode(current.payload);\n \n// Need to amend the json object to include additional values.\nparameters.opened_by = current.opened_by + \"\";\nparameters.requested_for = current.opened_for + \"\";\n \nanswer = new JSON().encode(parameters);\n```\n\n## View order guide failures\n\nOrder guide processing can fail, for example if the order guide being run does not exist. When a failure occurs during the order guide processing, the **Scriptable Order Guide Failures** submodule allows you to review and reprocess the failures. A record is created for each failure and once you fix the errors that caused the initial failure, you can reprocess the failed order guides.\n\n### About this task\n\nIf a failure occurs, a failure record is created in the Scriptable Order Guide Failures \\[sc\\_script\\_order\\_guide\\_failure\\] table.\n\nTo view details of a failure, navigate to **Service Catalog** &gt; **Catalog Administration** &gt; **Scriptable Order Guide Failures**, then open a failure record.\n\nReprocessing failures: If you have fixed the error that caused the initial failure, you can reprocess failed order guides.\n\n### Procedure\n\n1.  Navigate to **Service Catalog** &gt; **Catalog Administration** &gt; **Scriptable Order Guide Failures**.\n\n2.  Open the failure record.\n\n3.  Click the **Reprocess** related link.\n\n    To reprocess one or more failures:\n\n    1.  Navigate to **Service Catalog** &gt; **Catalog Administration** &gt; **Scriptable Order Guide Failures**.\n    2.  Select the check box beside one or more records to reprocess.\n    3.  Select **Reprocess** from the **Actions** choice list.\n\n"}}