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
@@ -1,13 +1,32 @@
# ServiceNow Script: Submit Catalog Item via Scheduled Job
# Submit catalog item (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.
This snippet demonstrates how to submit a Service Catalog item programmatically from server-side code using the `sn_sc.CartJS` API.

## 📜 Overview
## Purpose

Add an item to the Service Catalog cart with variables and perform checkout to create the request/req item records.

## Script behavior

- Creates a `sn_sc.CartJS()` cart instance.
- Calls `addToCart()` with `sysparm_id`, optional `sysparm_quantity`, optional `sysparm_requested_for`, and a `variables` object.
- Calls `checkoutCart()` to place the order.
- Logs success or error information using `gs.info`, `gs.warn`, and `gs.error`.

## Placeholders to replace

- `SYS_ID_OF_CAT_ITEM`: the `sys_id` of the Catalog Item (`sc_cat_item` record).
- `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.
- `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.

## Expected returns

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.

## Overview
1.Creates a cart instance with sn_sc.CartJS</br>
2.Adds a catalog item to the cart</br>
3.Places the order (checkout)</br>
4.Logs success or error messages

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

// 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');
var newItem = cart.addToCart({
// Replace 'SYS_ID_OF_CAT_ITEM' with the actual sys_id of the catalog item
"sysparm_id": "SYS_ID_OF_CAT_ITEM",
// Specify the quantity of the item
"sysparm_quantity": "1",
// Replace 'SYS_ID_OF_REQUESTED_FOR_USER' with the sys_id of the user for whom the item is requested
"sysparm_requested_for": "SYS_ID_OF_REQUESTED_FOR_USER",
// Add any necessary variables in the variables object
"variables": {
"variable_name1": "value",
"variable_name2": "value",
"variable_name3": "value"
}
});

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

} catch (e) {

}
catch (e) {
// Catch any unexpected errors and log them
gs.error('Unexpected error while submitting catalog item: ' + e.message);
}
}



Loading