File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Integration/RESTMessageV2/OpenAI Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments