Skip to content

Commit f900211

Browse files
authored
Add script to submit catalog item via scheduled job (#1889)
1 parent d3cde61 commit f900211

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ServiceNow Script: Submit Catalog Item via Scheduled Job
2+
3+
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.
4+
5+
## 📜 Overview
6+
7+
This script performs the following actions:
8+
9+
1. Generates a unique cart ID.
10+
2. Creates a new cart using the `Cart` API.
11+
3. Adds a catalog item to the cart (no variables required).
12+
4. Places the order.
13+
5. Logs success or error messages.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
try {
2+
// Generate a unique cart ID using GlideGuid
3+
var cartId = GlideGuid.generate(null);
4+
5+
// Create a new Cart object with the generated cart ID
6+
var cart = new Cart(cartId);
7+
8+
// Add a catalog item to the cart using its sys_id
9+
var item = cart.addItem('41725b2bc3503210e89b341d050131bc');
10+
11+
// Check if the item was successfully added
12+
if (!item) {
13+
gs.error('Failed to add catalog item to cart.');
14+
} else {
15+
// Place the order and get the resulting sc_request GlideRecord
16+
var rc = cart.placeOrder();
17+
18+
// Check if the order was successfully placed
19+
if (!rc) {
20+
gs.error('Failed to place order.');
21+
} else {
22+
// Log success message with the request number
23+
gs.info('Catalog item submitted successfully. Request Number: ' + rc.number);
24+
}
25+
}
26+
27+
} catch (e) {
28+
// Catch any unexpected errors and log them
29+
gs.error('Unexpected error while submitting catalog item: ' + e.message);
30+
}

0 commit comments

Comments
 (0)