Skip to content

Commit b4c69ba

Browse files
Create RestMessage.js
Build the prompt dynamically from the record in ServiceNow
1 parent c0877b3 commit b4c69ba

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
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+
//Script
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)