From aba90d6c7041f7841a26e02c4e65b9c802bab79c Mon Sep 17 00:00:00 2001 From: Ravi Gaurav Date: Tue, 14 Oct 2025 17:36:59 +0530 Subject: [PATCH 1/8] Create Catalog.js Catalog Builder API - A Scripted REST API to auto-create ServiceNow Catalog Items programmatically. --- .../Catalog.js | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js diff --git a/Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js b/Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js new file mode 100644 index 0000000000..d52fafb054 --- /dev/null +++ b/Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js @@ -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" + } + ] +} + From 45a3fcdd6a11143b6d3e83b6eadd3185a28329b9 Mon Sep 17 00:00:00 2001 From: Ravi Gaurav Date: Tue, 14 Oct 2025 17:39:29 +0530 Subject: [PATCH 2/8] Create README.md This readme file has info about how to Automate Catalog Item Creation with a Single REST Call. --- .../Create Catalog Item Dynamically/README.md | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md diff --git a/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md b/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md new file mode 100644 index 0000000000..600ccae368 --- /dev/null +++ b/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md @@ -0,0 +1,99 @@ +# 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. From c3c70a363353c19f8047f67f27941f0c67db52ac Mon Sep 17 00:00:00 2001 From: Ravi Gaurav Date: Tue, 14 Oct 2025 17:43:10 +0530 Subject: [PATCH 3/8] Update README.md Automate Catalog Item Creation with a Single REST Call --- .../Create Catalog Item Dynamically/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md b/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md index 600ccae368..4333f8ceb9 100644 --- a/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md +++ b/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md @@ -34,7 +34,7 @@ Using this API, they simply call one REST endpoint for each definition — no ma --- -## ⚙️ Scripted REST API Details +## Scripted REST API Details | Property | Value | |-----------|--------| @@ -57,7 +57,7 @@ Using this API, they simply call one REST endpoint for each definition — no ma --- -## 🧾 Example Input (POST Body) +## Example Input (POST Body) ```json { @@ -85,7 +85,7 @@ Using this API, they simply call one REST endpoint for each definition — no ma } -Example Output: +## Example Output: { "catalog_sys_id": "b2f6329cdb6d0010355b5fb4ca9619e2", "message": "Catalog item created successfully!" From 0c43396a6292cfee8cecee51a34827cc7f333bc6 Mon Sep 17 00:00:00 2001 From: Ravi Gaurav Date: Tue, 14 Oct 2025 17:45:55 +0530 Subject: [PATCH 4/8] Delete Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md --- .../Create Catalog Item Dynamically/README.md | 99 ------------------- 1 file changed, 99 deletions(-) delete mode 100644 Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md diff --git a/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md b/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md deleted file mode 100644 index 4333f8ceb9..0000000000 --- a/Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md +++ /dev/null @@ -1,99 +0,0 @@ -# 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. From 0bc1af4b02aba1d1b5f454b3c75aee5a030acb50 Mon Sep 17 00:00:00 2001 From: Ravi Gaurav Date: Tue, 14 Oct 2025 17:46:07 +0530 Subject: [PATCH 5/8] Delete Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js --- .../Catalog.js | 120 ------------------ 1 file changed, 120 deletions(-) delete mode 100644 Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js diff --git a/Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js b/Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js deleted file mode 100644 index d52fafb054..0000000000 --- a/Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js +++ /dev/null @@ -1,120 +0,0 @@ -// 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" - } - ] -} - From 380ea6d38279eb43b2128987d4470b1be1d26a42 Mon Sep 17 00:00:00 2001 From: Ravi Gaurav Date: Tue, 14 Oct 2025 20:20:17 +0530 Subject: [PATCH 6/8] Create chatGpt.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This project demonstrates how to integrate **ServiceNow** directly with **OpenAI’s ChatGPT** platform using only native features — no Scripted REST APIs, no IntegrationHub, and no plugins required. --- .../Business Rules/openAI/chatGpt.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Server-Side Components/Business Rules/openAI/chatGpt.js diff --git a/Server-Side Components/Business Rules/openAI/chatGpt.js b/Server-Side Components/Business Rules/openAI/chatGpt.js new file mode 100644 index 0000000000..defbdeebf7 --- /dev/null +++ b/Server-Side Components/Business Rules/openAI/chatGpt.js @@ -0,0 +1,41 @@ +/* Secnario : +Whenever a record (for example, an Incident) is created or updated, +ServiceNow will automatically send a text prompt (like short description or work notes) +to OpenAI ChatGPT API, get the response, and save it back to a field (like u_ai_summary)*/ + +----- +//Script + try { + // Build the prompt dynamically from the record + var userPrompt = "Summarize this issue in one sentence: " + current.short_description; + if (!userPrompt) return; + + // Prepare REST Message + var rest = new sn_ws.RESTMessageV2('OpenAI ChatGPT', 'POST'); // name & method + rest.setStringParameter('prompt', userPrompt); // replaces ${prompt} in body + + // Execute API call + var response = rest.execute(); + var responseBody = response.getBody(); + var status = response.getStatusCode(); + + if (status != 200) { + gs.error("OpenAI API error: " + responseBody); + return; + } + + // Parse and store response + var data = new global.JSON().decode(responseBody); + var aiResponse = data.choices[0].message.content; + + // Save to custom field (u_ai_summary) + current.u_ai_summary = aiResponse; + current.update(); + + gs.info("AI Summary added for incident: " + current.number); + + } catch (e) { + gs.error("Error calling OpenAI ChatGPT: " + e.message); + } + + From e21505a29abc0db0819f9a55ba444f4f18a86b54 Mon Sep 17 00:00:00 2001 From: Ravi Gaurav Date: Tue, 14 Oct 2025 20:22:24 +0530 Subject: [PATCH 7/8] Create README.md This readme file contain info about how to integrate ServiceNow with AI --- .../Business Rules/openAI/README.md | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Server-Side Components/Business Rules/openAI/README.md diff --git a/Server-Side Components/Business Rules/openAI/README.md b/Server-Side Components/Business Rules/openAI/README.md new file mode 100644 index 0000000000..b1fd67a984 --- /dev/null +++ b/Server-Side Components/Business Rules/openAI/README.md @@ -0,0 +1,92 @@ +# ServiceNow ↔ OpenAI ChatGPT Integration +**Connect ServiceNow with ChatGPT using REST Message and Business Rule** + +--- + +## Overview +This project demonstrates how to integrate **ServiceNow** directly with **OpenAI’s ChatGPT** platform using only native features - +no Scripted REST APIs, no IntegrationHub, and no plugins required. + +The integration leverages: +- A **REST Message** (calling OpenAI’s API endpoint) +- A **Business Rule** (triggering automatically on record updates) + +Once configured, ServiceNow can send ticket details (like short descriptions or work notes) to ChatGPT +and store AI-generated responses back in the record — such as incident summaries, resolution hints, or category suggestions. + +--- + +## Key Highlights + +100% Native - Uses **RESTMessageV2** and **Business Rule** only +Generates AI-based summaries, causes, or recommendations +Works with **GPT-3.5-Turbo**, **GPT-4-Turbo**, or **GPT-4o-Mini** models +Real-time execution after record creation or update +No external plugin dependencies + +--- + +## Use Case Example +Whenever a new **Incident** is created or updated, ServiceNow automatically: +1. Sends the incident’s short description and work notes to **ChatGPT**. +2. Receives an AI-generated summary or root cause statement. +3. Saves the response in a custom field (`u_ai_summary`). + +This automation reduces manual summarization and speeds up triage. + +--- + +## Prerequisites + +| Requirement | Description | +|--------------|-------------| +| **OpenAI API Key** | Obtain from [https://platform.openai.com](https://platform.openai.com) | +| **Model** | Any `gpt-4o-mini`, `gpt-4-turbo`, or `gpt-3.5-turbo` | +| **ServiceNow Role** | admin / developer | +| **Custom Field** | Add a field `u_ai_summary` (String or HTML) on `incident` table | + +--- + +## Step 1 — Create REST Message + +**Name:** `OpenAI ChatGPT` +**Endpoint:** `https://api.openai.com/v1/chat/completions` +**HTTP Method:** `POST` + +### Headers +| Name | Value | +|------|--------| +| Content-Type | application/json | +| Authorization | Bearer `YOUR_OPENAI_API_KEY` | + +### Request Body +```json +{ + "model": "gpt-4o-mini", + "messages": [ + { "role": "system", "content": "You are a helpful ITSM assistant." }, + { "role": "user", "content": "${prompt}" } + ] +} +## — Create Business Rule + +Table: incident +When: after insert or after update +Condition: short_description changes OR work_notes changes + +Example Input (Incident Data) +| Field | Value | +| ----------------- | ------------------------------------------------------ | +| Short Description | VPN not connecting for remote users | +| Work Notes | User reports frequent disconnections during peak hours | + +Example Response (from ChatGPT) +{ + "choices": [ + { + "message": { + "content": "Remote employees are unable to connect to the VPN consistently, possibly due to gateway timeout or bandwidth issues." + } + } + ] +} From cb77ede0d84ef866dac854040f96b8544c2f7092 Mon Sep 17 00:00:00 2001 From: Ravi Gaurav Date: Tue, 14 Oct 2025 20:23:14 +0530 Subject: [PATCH 8/8] Update chatGpt.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This project demonstrates how to integrate **ServiceNow** directly with **OpenAI’s ChatGPT** platform using only native features — no Scripted REST APIs, no IntegrationHub, and no plugins required. --- Server-Side Components/Business Rules/openAI/chatGpt.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server-Side Components/Business Rules/openAI/chatGpt.js b/Server-Side Components/Business Rules/openAI/chatGpt.js index defbdeebf7..08899adcd8 100644 --- a/Server-Side Components/Business Rules/openAI/chatGpt.js +++ b/Server-Side Components/Business Rules/openAI/chatGpt.js @@ -4,7 +4,7 @@ ServiceNow will automatically send a text prompt (like short description or work to OpenAI ChatGPT API, get the response, and save it back to a field (like u_ai_summary)*/ ----- -//Script +//Business Rule Script (After Insert) try { // Build the prompt dynamically from the record var userPrompt = "Summarize this issue in one sentence: " + current.short_description;