Skip to content

Commit d17e4ba

Browse files
authored
Add CartJS script to submit catalog items (#2064)
1 parent 3fdfa00 commit d17e4ba

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

Server-Side Components/Scheduled Jobs/Submit catalog item/README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
# ServiceNow Script: Submit Catalog Item via Scheduled Job
1+
# Submit catalog item (Scheduled Job)
22

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.
3+
This snippet demonstrates how to submit a Service Catalog item programmatically from server-side code using the `sn_sc.CartJS` API.
44

5-
## 📜 Overview
5+
## Purpose
6+
7+
Add an item to the Service Catalog cart with variables and perform checkout to create the request/req item records.
8+
9+
## Script behavior
10+
11+
- Creates a `sn_sc.CartJS()` cart instance.
12+
- Calls `addToCart()` with `sysparm_id`, optional `sysparm_quantity`, optional `sysparm_requested_for`, and a `variables` object.
13+
- Calls `checkoutCart()` to place the order.
14+
- Logs success or error information using `gs.info`, `gs.warn`, and `gs.error`.
15+
16+
## Placeholders to replace
17+
18+
- `SYS_ID_OF_CAT_ITEM`: the `sys_id` of the Catalog Item (`sc_cat_item` record).
19+
- `SYS_ID_OF_REQUESTED_FOR_USER`: (optional) the `sys_id` of the user to set as Requested For. Remove the property to default to the current user.
20+
- `variable_name1`, `variable_name2`, `variable_nameN` :variable names expected by the catalog item. Use the variable name (not label) unless your instance expects variable IDs.
21+
22+
## Expected returns
23+
24+
You will receive an object containing the request number representating of the created `sc_request` with a `sc_req_item` Item. If unsure, log `JSON.stringify(checkoutInfo)` to inspect.
25+
26+
## Overview
27+
1.Creates a cart instance with sn_sc.CartJS</br>
28+
2.Adds a catalog item to the cart</br>
29+
3.Places the order (checkout)</br>
30+
4.Logs success or error messages
631

7-
This script performs the following actions:
832

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 & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
1+
// This script submits a catalog item to the ServiceNow Service Catalog using the CartJS API.
2+
// It adds the item to the cart with specified variables and then checks out the cart.
13
try {
2-
// Generate a unique cart ID using GlideGuid
3-
var cartId = GlideGuid.generate(null);
4+
var cart = new sn_sc.CartJS();
5+
// Add the catalog item to the cart
46

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');
7+
var newItem = cart.addToCart({
8+
// Replace 'SYS_ID_OF_CAT_ITEM' with the actual sys_id of the catalog item
9+
"sysparm_id": "SYS_ID_OF_CAT_ITEM",
10+
// Specify the quantity of the item
11+
"sysparm_quantity": "1",
12+
// Replace 'SYS_ID_OF_REQUESTED_FOR_USER' with the sys_id of the user for whom the item is requested
13+
"sysparm_requested_for": "SYS_ID_OF_REQUESTED_FOR_USER",
14+
// Add any necessary variables in the variables object
15+
"variables": {
16+
"variable_name1": "value",
17+
"variable_name2": "value",
18+
"variable_name3": "value"
19+
}
20+
});
1021

11-
// Check if the item was successfully added
12-
if (!item) {
13-
gs.error('Failed to add catalog item to cart.');
22+
if (newItem) {
23+
// Checkout the cart to submit the order
24+
var checkoutInfo = cart.checkoutCart();
25+
gs.info('Catalog item submitted successfully: ' + JSON.stringify(checkoutInfo));
1426
} 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-
}
27+
gs.warn('Failed to add catalog item to cart.');
2528
}
2629

27-
} catch (e) {
30+
31+
}
32+
catch (e) {
2833
// Catch any unexpected errors and log them
2934
gs.error('Unexpected error while submitting catalog item: ' + e.message);
30-
}
35+
}
36+
37+
38+

0 commit comments

Comments
 (0)