diff --git a/Server-Side Components/Server Side/Create Catalog Request Programmatically/README.md b/Server-Side Components/Server Side/Create Catalog Request Programmatically/README.md new file mode 100644 index 0000000000..55c8b767dc --- /dev/null +++ b/Server-Side Components/Server Side/Create Catalog Request Programmatically/README.md @@ -0,0 +1,29 @@ +# Create Service Catalog Request Programmatically + +## Description +This snippet demonstrates how to programmatically create a Service Catalog request using the CartJS API from server-side code. It bypasses the Service Portal interface and directly adds items to a cart, populates variables, and completes the checkout process. + +## Use Cases +- Automated hardware provisioning workflows +- Integration endpoints that trigger catalog requests +- Business Rules that create catalog items based on conditions +- Scheduled Jobs that batch-create catalog requests +- UI Actions that automate catalog submissions +- Custom approval processes that generate follow-up requests + +## Prerequisites +- Service Catalog Scoped API plugin enabled (com.glideapp.servicecatalog.scoped.api) +- Valid catalog item sys_id +- Variable names must match exactly as defined in the catalog item + +## Key Features +- Populates catalog item variables dynamically +- Automatically checks out the cart +- Returns request ID and number for reference +- Can be used in any server-side context (Business Rules, Script Includes, etc.) + +## Notes +- Two-step checkout setting affects the return value +- If two-step checkout is enabled, returns cart_id +- If two-step checkout is disabled, returns request_id and request_number +- Replace placeholder sys_ids with actual values from your instance diff --git a/Server-Side Components/Server Side/Create Catalog Request Programmatically/cartjs_create_request.js b/Server-Side Components/Server Side/Create Catalog Request Programmatically/cartjs_create_request.js new file mode 100644 index 0000000000..23d3562243 --- /dev/null +++ b/Server-Side Components/Server Side/Create Catalog Request Programmatically/cartjs_create_request.js @@ -0,0 +1,48 @@ +// Create catalog requests programmatically using CartJS API +// Tested in background script on dev instance - successfully created REQ records +// Works for automation workflows and bulk operations + +function createCatalogRequest(catalogItemSysId, requestedFor, requestData) { + var cart = new sn_sc.CartJS(); + + var item = { + "sysparm_id": catalogItemSysId, + "sysparm_quantity": "1", + "variables": requestData + }; + + cart.addToCart(item); + var result = cart.checkoutCart(); + + return { + request_id: result.request_id, + request_number: result.request_number + }; +} + +// Basic usage example +var catalogItem = 'YOUR_CATALOG_ITEM_SYS_ID'; +var user = gs.getUserID(); +var data = { + "short_description": "Automated request" +}; + +var req = createCatalogRequest(catalogItem, user, data); +gs.info('Created: ' + req.request_number); + +// Example with CI reference +var req = createCatalogRequest(catalogItem, current.assigned_to, { + "configuration_item": current.sys_id, + "short_description": "Request for " + current.name +}); + +// Get the generated task if needed +var req = createCatalogRequest(catalogItem, user, data); +if (req.request_id) { + var gr = new GlideRecord('sc_task'); + gr.addQuery('request', req.request_id); + gr.query(); + if (gr.next()) { + gs.info('Task: ' + gr.number); + } +}