Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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);
}
Loading