{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":604,"path":"api-reference/scripts/c_ScptblAsmtExctnPlns.md","title":"Scriptable assignment of execution plans","bundle":null,"doc_type":null,"release":"australia","body":"\n# Scriptable assignment of execution plans\n\nEach catalog item has an associated execution plan, used whenever an item of that type is ordered; if no plan is specified, the default plan is used. This default is effective for most organizations, but your execution plan may need to vary based on additional criteria.\n\nFor example, in the base system service catalog, a request for a new PC always uses the PC Delivery Plan. However, this plan may need to vary for unusual circumstances - such as when a requester is working from home, at a remote location.\n\nTo provide this flexibility, you can use a script to override the default execution plan on a specific catalog item.\n\n**Parent Topic:**[Server-side scripting](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_ServerScripting.md)\n\n## Limitations during script execution\n\nExecution plan scripts have limitations that need to be considered during their implementation.\n\nWhile the execution plan script runs:\n\n-   You cannot interact with any catalog tasks as catalog tasks are only created after the execution plan is selected.\n-   Some fields such as **total delivery time** and **due date** are not yet calculated, although the request itself is available within the script via current.request\\(\\).\n-   Approvals have not yet been generated.\n\n## Writing the scripts\n\nFollow these guidelines when writing execution plan scripts.\n\nExecution plan scripts can access the same global variables and other functions as in any other server side execution plan.\n\n-   current is the currently-requested catalog item, `sc_req_item`.\n-   current.delivery\\_plan\\(\\) is the assigned execution plan for this catalog item.\n\nThe evaluated value from the script is used as the `sys_id` of the execution plan.\n\nSimple example:\n\n```\ncurrent.delivery_plan.setDisplayValue('PC Delivery Plan')\n```\n\nIf an invalid value is returned, such as undefined or not found, then the existing assigned value is used.\n\nMore complex example:\n\n```\ngetexecutionplan();\nfunction getexecutionplan() {\nvar location = current.request.requested_for.location.getDisplayValue();\n// if we're in Atlanta\nif (location == 'Atlanta') {\n   // use the remote pc delivery plan instead of the normal one\n   var remote_plan = new GlideRecord('sc_cat_item_delivery_plan');\n   remote_plan.addQuery('name', 'Remote PC Delivery Plan');\n   remote_plan.query();\n   remote_plan.next();\n   current.delivery_plan = remote_plan.sys_id;\n   return remote_plan_sys_id;\n   } \n   return current_delivery_plan;\n}\n```\n\nIn this example, any time a request is for a user in Atlanta, ServiceNow uses the Remote PC Delivery Plan. Otherwise, the execution plan is not overridden and ServiceNow uses the catalog item's normal execution plan, the PC Delivery Plan.\n\n## Add a script to a catalog item\n\nYou can add a script to a catalog item so that the script runs each time a user requests that item.\n\n### Before you begin\n\nRole required: admin\n\n### Procedure\n\n1.  Navigate to **All** &gt; **Service Catalog** &gt; **Maintain Items**.\n\n2.  Select the relevant catalog item to which you wish to add the script.\n\n3.  Configure the catalog item form to add the execution plan script field, often named **Delivery Plan Script**.\n\n4.  Fill in the script details.\n\n5.  Update the item form with your changes.\n\n\n### Result\n\nThe script runs each time that item is requested, selecting the execution plan to run with that item.\n\n## Use a script to approve an execution plan\n\nYou can use an approval rule script to approve an execution plan.\n\n### Before you begin\n\nRole required: admin\n\n### Procedure\n\n1.  In the filter navigator, enter `sc_cat_item_dt_approval.list` to retrieve an approval execution plan task.\n\n2.  From the form context menu, select **View** &gt; **Plan**.\n\n3.  In the Approval Script field, enter an approval script using the same syntax and rules you would use on an approval rule.\n\n\n### Script specifying the approver\n\nIn this example, the requester’s manager is the approver.\n\n```javascript\n(function manager_as_approver () {\nvar request = current.request_item.request;\nvar rc = request.requested_for.manager;\nreturn rc;\n})();\n```\n\n"}}