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
@@ -0,0 +1,100 @@

# ServiceNow Catalog Builder API
**Automate Catalog Item Creation with a Single REST Call**

---

## Overview

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.

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**.

---

## Key Features

Automatically create **Catalog Items** in `sc_cat_item`
Dynamically generate **Variables** and **Choices**
Supports **category mapping** and **item ownership**
Extensible design for **flows, icons, and attachments**
Developer-friendly — fully JSON-driven

---

## Use Case

This API is perfect for:
- **Admin Automation:** Auto-build standard catalog forms during environment setup.
- **RPA / CI Pipelines:** Integrate with DevOps or GitHub Actions to deploy catalog definitions.
- **Dynamic Service Portals:** Allow external apps or portals to create items on demand.

Example:
A company wants to auto-create 10 new service catalog items from a GitHub configuration file.
Using this API, they simply call one REST endpoint for each definition — no manual clicks needed.

---

## Scripted REST API Details

| Property | Value |
|-----------|--------|
| **Name** | Catalog Builder API |
| **API ID** | `x_demo.catalog_creator` |
| **Resource Path** | `/create` |
| **Method** | POST |
| **Authentication** | Basic Auth / OAuth |
| **Tables Used** | `sc_cat_item`, `item_option_new`, `question_choice` |

---

## Logic Flow

1. **Receive JSON input** with item name, category, and variables.
2. **Create a new record** in `sc_cat_item`.
3. **Loop through variables** and create them in `item_option_new`.
4. If the variable type is `select_box`, create **choices** automatically.
5. Return a JSON response with the new item’s `sys_id` and success message.

---

## Example Input (POST Body)

```json
{
"name": "Request New Laptop",
"category": "Hardware",
"short_description": "Laptop provisioning request form",
"description": "Allows employees to request a new laptop with model and RAM options.",
"owner": "admin",
"variables": [
{
"name": "Laptop Model",
"type": "select_box",
"choices": "Dell,HP,Lenovo"
},
{
"name": "RAM Size",
"type": "select_box",
"choices": "8GB,16GB,32GB"
},
{
"name": "Business Justification",
"type": "multi_line_text"
}
]
}


## Example Output:
{
"catalog_sys_id": "b2f6329cdb6d0010355b5fb4ca9619e2",
"message": "Catalog item created successfully!"
}
After the API call:
A new Catalog Item appears under Maintain Items.
The item contains:
Short Description: Laptop provisioning form
Variables: Laptop Model, RAM Size, Business Justification
Choices: Auto-populated for select boxes
The item is active and ready to use in the catalog.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Scenario : As a ServiceNow Admin or Developer managing dozens of similar request forms (like “Request Laptop”, “Request Mobile”, “Request Access”, etc.).
// Manually creating each catalog item is repetitive.

// This code will Automate Catalog Item Creation with a Single REST Call
//Script: POST /api/x_demo/catalog_creator/create

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var body = request.body.data;
var result = {};

try {
// 1. Create Catalog Item
var catItem = new GlideRecord('sc_cat_item');
catItem.initialize();
catItem.name = body.name;
catItem.short_description = body.short_description || '';
catItem.description = body.description || '';
catItem.category = getCategorySysId(body.category);
catItem.owning_group = getOwner(body.owner);
catItem.active = true;
var catSysId = catItem.insert();

result.catalog_sys_id = catSysId;

// 2. Create Variables
if (body.variables && body.variables.length > 0) {
for (var i = 0; i < body.variables.length; i++) {
var v = body.variables[i];

var variable = new GlideRecord('item_option_new');
variable.initialize();
variable.cat_item = catSysId;
variable.name = v.name.toLowerCase().replace(/ /g, '_');
variable.question_text = v.name;
variable.type = getType(v.type);
variable.order = (i + 1) * 100;
var varSysId = variable.insert();

// Add choices for select box variables
if (v.choices && v.choices.length > 0) {
var choices = v.choices.split(',');
for (var j = 0; j < choices.length; j++) {
var choice = new GlideRecord('question_choice');
choice.initialize();
choice.question = varSysId;
choice.value = choices[j].trim();
choice.label = choices[j].trim();
choice.insert();
}
}
}
}

result.message = "Catalog item created successfully!";
response.setStatus(201);

} catch (e) {
gs.error("Error creating catalog item: " + e);
result.message = e.toString();
response.setStatus(500);
}

response.setBody(result);


function getCategorySysId(categoryName) {
var cat = new GlideRecord('sc_category');
cat.addQuery('title', categoryName);
cat.query();
if (cat.next()) return cat.sys_id;
return null;
}

function getOwner(ownerName) {
var usr = new GlideRecord('sys_user');
usr.addQuery('user_name', ownerName);
usr.query();
if (usr.next()) return usr.sys_id;
return gs.getUserID();
}

function getType(typeName) {
var map = {
"single_line_text": 1,
"multi_line_text": 2,
"select_box": 3,
"reference": 8,
"checkbox": 5
};
return map[typeName] || 1;
}

})(request, response);

//Example JSON
//{
"name": "Request New Laptop",
"category": "Hardware",
"short_description": "Laptop provisioning form",
"description": "Allows employees to request a new laptop.",
"owner": "admin",
"variables": [
{
"name": "Laptop Model",
"type": "select_box",
"choices": "Dell,HP,Lenovo"
},
{
"name": "RAM Size",
"type": "select_box",
"choices": "8GB,16GB,32GB"
},
{
"name": "Business Justification",
"type": "multi_line_text"
}
]
}

Loading