Skip to content
Closed
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,29 @@
# Create Service Catalog Request Programmatically

## Description
This snippet demonstrates how to programmatically create a Service Catalog request using the CartJS API from server-side code. It bypasses the Service Portal interface and directly adds items to a cart, populates variables, and completes the checkout process.

## Use Cases
- Automated hardware provisioning workflows
- Integration endpoints that trigger catalog requests
- Business Rules that create catalog items based on conditions
- Scheduled Jobs that batch-create catalog requests
- UI Actions that automate catalog submissions
- Custom approval processes that generate follow-up requests

## Prerequisites
- Service Catalog Scoped API plugin enabled (com.glideapp.servicecatalog.scoped.api)
- Valid catalog item sys_id
- Variable names must match exactly as defined in the catalog item

## Key Features
- Populates catalog item variables dynamically
- Automatically checks out the cart
- Returns request ID and number for reference
- Can be used in any server-side context (Business Rules, Script Includes, etc.)

## Notes
- Two-step checkout setting affects the return value
- If two-step checkout is enabled, returns cart_id
- If two-step checkout is disabled, returns request_id and request_number
- Replace placeholder sys_ids with actual values from your instance
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Create catalog requests programmatically using CartJS API
// Tested in background script on dev instance - successfully created REQ records
// Works for automation workflows and bulk operations

function createCatalogRequest(catalogItemSysId, requestedFor, requestData) {
var cart = new sn_sc.CartJS();

var item = {
"sysparm_id": catalogItemSysId,
"sysparm_quantity": "1",
"variables": requestData
};

cart.addToCart(item);
var result = cart.checkoutCart();

return {
request_id: result.request_id,
request_number: result.request_number
};
}

// Basic usage example
var catalogItem = 'YOUR_CATALOG_ITEM_SYS_ID';
var user = gs.getUserID();
var data = {
"short_description": "Automated request"
};

var req = createCatalogRequest(catalogItem, user, data);
gs.info('Created: ' + req.request_number);

// Example with CI reference
var req = createCatalogRequest(catalogItem, current.assigned_to, {
"configuration_item": current.sys_id,
"short_description": "Request for " + current.name
});

// Get the generated task if needed
var req = createCatalogRequest(catalogItem, user, data);
if (req.request_id) {
var gr = new GlideRecord('sc_task');
gr.addQuery('request', req.request_id);
gr.query();
if (gr.next()) {
gs.info('Task: ' + gr.number);
}
}
Loading