Skip to content

Commit 34494f1

Browse files
ServiceNow to OpenAI Integration (#2151)
* 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/README.md * Delete Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js * Create chatGpt.js 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. * Create README.md This readme file contain info about how to integrate ServiceNow with AI * Update chatGpt.js 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.
1 parent 02eaef8 commit 34494f1

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# ServiceNow ↔ OpenAI ChatGPT Integration
2+
**Connect ServiceNow with ChatGPT using REST Message and Business Rule**
3+
4+
---
5+
6+
## Overview
7+
This project demonstrates how to integrate **ServiceNow** directly with **OpenAI’s ChatGPT** platform using only native features -
8+
no Scripted REST APIs, no IntegrationHub, and no plugins required.
9+
10+
The integration leverages:
11+
- A **REST Message** (calling OpenAI’s API endpoint)
12+
- A **Business Rule** (triggering automatically on record updates)
13+
14+
Once configured, ServiceNow can send ticket details (like short descriptions or work notes) to ChatGPT
15+
and store AI-generated responses back in the record — such as incident summaries, resolution hints, or category suggestions.
16+
17+
---
18+
19+
## Key Highlights
20+
21+
100% Native - Uses **RESTMessageV2** and **Business Rule** only
22+
Generates AI-based summaries, causes, or recommendations
23+
Works with **GPT-3.5-Turbo**, **GPT-4-Turbo**, or **GPT-4o-Mini** models
24+
Real-time execution after record creation or update
25+
No external plugin dependencies
26+
27+
---
28+
29+
## Use Case Example
30+
Whenever a new **Incident** is created or updated, ServiceNow automatically:
31+
1. Sends the incident’s short description and work notes to **ChatGPT**.
32+
2. Receives an AI-generated summary or root cause statement.
33+
3. Saves the response in a custom field (`u_ai_summary`).
34+
35+
This automation reduces manual summarization and speeds up triage.
36+
37+
---
38+
39+
## Prerequisites
40+
41+
| Requirement | Description |
42+
|--------------|-------------|
43+
| **OpenAI API Key** | Obtain from [https://platform.openai.com](https://platform.openai.com) |
44+
| **Model** | Any `gpt-4o-mini`, `gpt-4-turbo`, or `gpt-3.5-turbo` |
45+
| **ServiceNow Role** | admin / developer |
46+
| **Custom Field** | Add a field `u_ai_summary` (String or HTML) on `incident` table |
47+
48+
---
49+
50+
## Step 1 — Create REST Message
51+
52+
**Name:** `OpenAI ChatGPT`
53+
**Endpoint:** `https://api.openai.com/v1/chat/completions`
54+
**HTTP Method:** `POST`
55+
56+
### Headers
57+
| Name | Value |
58+
|------|--------|
59+
| Content-Type | application/json |
60+
| Authorization | Bearer `YOUR_OPENAI_API_KEY` |
61+
62+
### Request Body
63+
```json
64+
{
65+
"model": "gpt-4o-mini",
66+
"messages": [
67+
{ "role": "system", "content": "You are a helpful ITSM assistant." },
68+
{ "role": "user", "content": "${prompt}" }
69+
]
70+
}
71+
## — Create Business Rule
72+
73+
Table: incident
74+
When: after insert or after update
75+
Condition: short_description changes OR work_notes changes
76+
77+
Example Input (Incident Data)
78+
| Field | Value |
79+
| ----------------- | ------------------------------------------------------ |
80+
| Short Description | VPN not connecting for remote users |
81+
| Work Notes | User reports frequent disconnections during peak hours |
82+
83+
Example Response (from ChatGPT)
84+
{
85+
"choices": [
86+
{
87+
"message": {
88+
"content": "Remote employees are unable to connect to the VPN consistently, possibly due to gateway timeout or bandwidth issues."
89+
}
90+
}
91+
]
92+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Secnario :
2+
Whenever a record (for example, an Incident) is created or updated,
3+
ServiceNow will automatically send a text prompt (like short description or work notes)
4+
to OpenAI ChatGPT API, get the response, and save it back to a field (like u_ai_summary)*/
5+
6+
-----
7+
//Business Rule Script (After Insert)
8+
try {
9+
// Build the prompt dynamically from the record
10+
var userPrompt = "Summarize this issue in one sentence: " + current.short_description;
11+
if (!userPrompt) return;
12+
13+
// Prepare REST Message
14+
var rest = new sn_ws.RESTMessageV2('OpenAI ChatGPT', 'POST'); // name & method
15+
rest.setStringParameter('prompt', userPrompt); // replaces ${prompt} in body
16+
17+
// Execute API call
18+
var response = rest.execute();
19+
var responseBody = response.getBody();
20+
var status = response.getStatusCode();
21+
22+
if (status != 200) {
23+
gs.error("OpenAI API error: " + responseBody);
24+
return;
25+
}
26+
27+
// Parse and store response
28+
var data = new global.JSON().decode(responseBody);
29+
var aiResponse = data.choices[0].message.content;
30+
31+
// Save to custom field (u_ai_summary)
32+
current.u_ai_summary = aiResponse;
33+
current.update();
34+
35+
gs.info("AI Summary added for incident: " + current.number);
36+
37+
} catch (e) {
38+
gs.error("Error calling OpenAI ChatGPT: " + e.message);
39+
}
40+
41+

0 commit comments

Comments
 (0)