diff --git a/Server-Side Components/Scheduled Jobs/Submit catalog item/README.md b/Server-Side Components/Scheduled Jobs/Submit catalog item/README.md new file mode 100644 index 0000000000..f75eee39e9 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Submit catalog item/README.md @@ -0,0 +1,13 @@ +# ServiceNow Script: Submit Catalog Item via Scheduled Job + +This script demonstrates how to programmatically submit a ServiceNow **catalog item** using the `Cart` API. It is designed to be run as a **Background Script** or adapted into a **Scheduled Job**. The script includes error handling and logging, and can be extended to support additional use cases such as variable injection or duplicate request prevention. + +## 📜 Overview + +This script performs the following actions: + +1. Generates a unique cart ID. +2. Creates a new cart using the `Cart` API. +3. Adds a catalog item to the cart (no variables required). +4. Places the order. +5. Logs success or error messages. \ No newline at end of file diff --git a/Server-Side Components/Scheduled Jobs/Submit catalog item/submit_catalog_item.js b/Server-Side Components/Scheduled Jobs/Submit catalog item/submit_catalog_item.js new file mode 100644 index 0000000000..d5d89ba1f9 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Submit catalog item/submit_catalog_item.js @@ -0,0 +1,30 @@ +try { + // Generate a unique cart ID using GlideGuid + var cartId = GlideGuid.generate(null); + + // Create a new Cart object with the generated cart ID + var cart = new Cart(cartId); + + // Add a catalog item to the cart using its sys_id + var item = cart.addItem('41725b2bc3503210e89b341d050131bc'); + + // Check if the item was successfully added + if (!item) { + gs.error('Failed to add catalog item to cart.'); + } else { + // Place the order and get the resulting sc_request GlideRecord + var rc = cart.placeOrder(); + + // Check if the order was successfully placed + if (!rc) { + gs.error('Failed to place order.'); + } else { + // Log success message with the request number + gs.info('Catalog item submitted successfully. Request Number: ' + rc.number); + } + } + +} catch (e) { + // Catch any unexpected errors and log them + gs.error('Unexpected error while submitting catalog item: ' + e.message); +} \ No newline at end of file