diff --git a/Server-Side Components/Background Scripts/Create Request through script/README.md b/Server-Side Components/Background Scripts/Create Request through script/README.md new file mode 100644 index 0000000000..3f07699f99 --- /dev/null +++ b/Server-Side Components/Background Scripts/Create Request through script/README.md @@ -0,0 +1,6 @@ +# ServiceNow Background Script – Create RITM via sn_sc.CartJS() + +## Overview +This script demonstrates how to programmatically create a **Request Item (RITM)** and **Request (REQ)** in ServiceNow using the modern **`sn_sc.CartJS()` API**. + +The script simulates a Service Catalog order submission — adding a catalog item to the cart and checking it out. This ensures that all **catalog flows, workflows, and business rules** are triggered exactly as if the request were placed through the Service Portal or native UI. diff --git a/Server-Side Components/Background Scripts/Create Request through script/create_request_using_script.js b/Server-Side Components/Background Scripts/Create Request through script/create_request_using_script.js new file mode 100644 index 0000000000..7df7e10663 --- /dev/null +++ b/Server-Side Components/Background Scripts/Create Request through script/create_request_using_script.js @@ -0,0 +1,24 @@ + +var cart = new sn_sc.CartJS(); +//define variables +var itemDetails = { + 'sysparm_id': 'catalog_item_sys_id', + 'variables': { + 'requested_for': 'user_sys_id' + /* + . + . + remaining variables + . + . + */ + } +}; + +// Add item to cart (returns item sys_id within the cart) +var cartItem = cart.addToCart(itemDetails); +gs.info('Added catalog item to cart: ' + cartItem); + +// -- Place the order --- +var order = cart.checkoutCart(); +gs.info('Order placed successfully. Request Sys ID: ' + order.request_id);