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
92 changes: 92 additions & 0 deletions Server-Side Components/Business Rules/openAI/README.md
Original file line number Diff line number Diff line change
@@ -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."
}
}
]
}
41 changes: 41 additions & 0 deletions Server-Side Components/Business Rules/openAI/chatGpt.js
Original file line number Diff line number Diff line change
@@ -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)*/

-----
//Business Rule Script (After Insert)
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);
}


Loading