Skip to content

Commit 5799b94

Browse files
Dynamically creates Service Catalog using Scripted Rest API (#2143)
* Create Catalog.js Catalog Builder API - A Scripted REST API to auto-create ServiceNow Catalog Items programmatically. * Create README.md This readme file has info about how to Automate Catalog Item Creation with a Single REST Call. * Update README.md Automate Catalog Item Creation with a Single REST Call * Delete Integration/Scripted REST Api/Create Catalog Item Dynamically directory * Create catalog.js This project demonstrates how to create a new Service Catalog Item in ServiceNow dynamically using a Scripted REST API. * Create README.md The **ServiceNow Catalog Builder API** is a custom **Scripted REST API** that dynamically creates Service Catalog Items in your instance including variables and choices from a simple JSON payload.
1 parent b0ff8ee commit 5799b94

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
# ServiceNow Catalog Builder API
3+
**Automate Catalog Item Creation with a Single REST Call**
4+
5+
---
6+
7+
## Overview
8+
9+
The **ServiceNow Catalog Builder API** is a custom **Scripted REST API** that dynamically creates Service Catalog Items in your instance — including variables and choices — from a simple JSON payload.
10+
11+
This API eliminates the repetitive manual work of configuring Catalog Items one by one, and makes it possible to **automate catalog creation programmatically** or **integrate it with CI/CD pipelines, GitHub workflows, or external systems**.
12+
13+
---
14+
15+
## Key Features
16+
17+
Automatically create **Catalog Items** in `sc_cat_item`
18+
Dynamically generate **Variables** and **Choices**
19+
Supports **category mapping** and **item ownership**
20+
Extensible design for **flows, icons, and attachments**
21+
Developer-friendly — fully JSON-driven
22+
23+
---
24+
25+
## Use Case
26+
27+
This API is perfect for:
28+
- **Admin Automation:** Auto-build standard catalog forms during environment setup.
29+
- **RPA / CI Pipelines:** Integrate with DevOps or GitHub Actions to deploy catalog definitions.
30+
- **Dynamic Service Portals:** Allow external apps or portals to create items on demand.
31+
32+
Example:
33+
A company wants to auto-create 10 new service catalog items from a GitHub configuration file.
34+
Using this API, they simply call one REST endpoint for each definition — no manual clicks needed.
35+
36+
---
37+
38+
## Scripted REST API Details
39+
40+
| Property | Value |
41+
|-----------|--------|
42+
| **Name** | Catalog Builder API |
43+
| **API ID** | `x_demo.catalog_creator` |
44+
| **Resource Path** | `/create` |
45+
| **Method** | POST |
46+
| **Authentication** | Basic Auth / OAuth |
47+
| **Tables Used** | `sc_cat_item`, `item_option_new`, `question_choice` |
48+
49+
---
50+
51+
## Logic Flow
52+
53+
1. **Receive JSON input** with item name, category, and variables.
54+
2. **Create a new record** in `sc_cat_item`.
55+
3. **Loop through variables** and create them in `item_option_new`.
56+
4. If the variable type is `select_box`, create **choices** automatically.
57+
5. Return a JSON response with the new item’s `sys_id` and success message.
58+
59+
---
60+
61+
## Example Input (POST Body)
62+
63+
```json
64+
{
65+
"name": "Request New Laptop",
66+
"category": "Hardware",
67+
"short_description": "Laptop provisioning request form",
68+
"description": "Allows employees to request a new laptop with model and RAM options.",
69+
"owner": "admin",
70+
"variables": [
71+
{
72+
"name": "Laptop Model",
73+
"type": "select_box",
74+
"choices": "Dell,HP,Lenovo"
75+
},
76+
{
77+
"name": "RAM Size",
78+
"type": "select_box",
79+
"choices": "8GB,16GB,32GB"
80+
},
81+
{
82+
"name": "Business Justification",
83+
"type": "multi_line_text"
84+
}
85+
]
86+
}
87+
88+
89+
## Example Output:
90+
{
91+
"catalog_sys_id": "b2f6329cdb6d0010355b5fb4ca9619e2",
92+
"message": "Catalog item created successfully!"
93+
}
94+
After the API call:
95+
A new Catalog Item appears under Maintain Items.
96+
The item contains:
97+
Short Description: Laptop provisioning form
98+
Variables: Laptop Model, RAM Size, Business Justification
99+
Choices: Auto-populated for select boxes
100+
The item is active and ready to use in the catalog.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Scenario : As a ServiceNow Admin or Developer managing dozens of similar request forms (like “Request Laptop”, “Request Mobile”, “Request Access”, etc.).
2+
// Manually creating each catalog item is repetitive.
3+
4+
// This code will Automate Catalog Item Creation with a Single REST Call
5+
//Script: POST /api/x_demo/catalog_creator/create
6+
7+
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
8+
9+
var body = request.body.data;
10+
var result = {};
11+
12+
try {
13+
// 1. Create Catalog Item
14+
var catItem = new GlideRecord('sc_cat_item');
15+
catItem.initialize();
16+
catItem.name = body.name;
17+
catItem.short_description = body.short_description || '';
18+
catItem.description = body.description || '';
19+
catItem.category = getCategorySysId(body.category);
20+
catItem.owning_group = getOwner(body.owner);
21+
catItem.active = true;
22+
var catSysId = catItem.insert();
23+
24+
result.catalog_sys_id = catSysId;
25+
26+
// 2. Create Variables
27+
if (body.variables && body.variables.length > 0) {
28+
for (var i = 0; i < body.variables.length; i++) {
29+
var v = body.variables[i];
30+
31+
var variable = new GlideRecord('item_option_new');
32+
variable.initialize();
33+
variable.cat_item = catSysId;
34+
variable.name = v.name.toLowerCase().replace(/ /g, '_');
35+
variable.question_text = v.name;
36+
variable.type = getType(v.type);
37+
variable.order = (i + 1) * 100;
38+
var varSysId = variable.insert();
39+
40+
// Add choices for select box variables
41+
if (v.choices && v.choices.length > 0) {
42+
var choices = v.choices.split(',');
43+
for (var j = 0; j < choices.length; j++) {
44+
var choice = new GlideRecord('question_choice');
45+
choice.initialize();
46+
choice.question = varSysId;
47+
choice.value = choices[j].trim();
48+
choice.label = choices[j].trim();
49+
choice.insert();
50+
}
51+
}
52+
}
53+
}
54+
55+
result.message = "Catalog item created successfully!";
56+
response.setStatus(201);
57+
58+
} catch (e) {
59+
gs.error("Error creating catalog item: " + e);
60+
result.message = e.toString();
61+
response.setStatus(500);
62+
}
63+
64+
response.setBody(result);
65+
66+
67+
function getCategorySysId(categoryName) {
68+
var cat = new GlideRecord('sc_category');
69+
cat.addQuery('title', categoryName);
70+
cat.query();
71+
if (cat.next()) return cat.sys_id;
72+
return null;
73+
}
74+
75+
function getOwner(ownerName) {
76+
var usr = new GlideRecord('sys_user');
77+
usr.addQuery('user_name', ownerName);
78+
usr.query();
79+
if (usr.next()) return usr.sys_id;
80+
return gs.getUserID();
81+
}
82+
83+
function getType(typeName) {
84+
var map = {
85+
"single_line_text": 1,
86+
"multi_line_text": 2,
87+
"select_box": 3,
88+
"reference": 8,
89+
"checkbox": 5
90+
};
91+
return map[typeName] || 1;
92+
}
93+
94+
})(request, response);
95+
96+
//Example JSON
97+
//{
98+
"name": "Request New Laptop",
99+
"category": "Hardware",
100+
"short_description": "Laptop provisioning form",
101+
"description": "Allows employees to request a new laptop.",
102+
"owner": "admin",
103+
"variables": [
104+
{
105+
"name": "Laptop Model",
106+
"type": "select_box",
107+
"choices": "Dell,HP,Lenovo"
108+
},
109+
{
110+
"name": "RAM Size",
111+
"type": "select_box",
112+
"choices": "8GB,16GB,32GB"
113+
},
114+
{
115+
"name": "Business Justification",
116+
"type": "multi_line_text"
117+
}
118+
]
119+
}
120+

0 commit comments

Comments
 (0)