Skip to content

Commit 08a689f

Browse files
Create script.js
1 parent 6750b61 commit 08a689f

File tree

1 file changed

+78
-0
lines changed
  • Core ServiceNow APIs/GlideJsonPath/Basic-Example/Creating a P1 Major Incident from an external monitoring system

1 file changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
try {
2+
// Get the JSON payload from the request body.
3+
var requestBody = request.body.dataString;
4+
5+
// Use GlideJsonPath to parse the JSON payload efficiently.
6+
var gjp = new GlideJsonPath(requestBody);
7+
8+
// Extract key information from the JSON payload.
9+
var severity = gjp.read("$.alert.severity");
10+
var shortDescription = gjp.read("$.alert.description");
11+
var source = gjp.read("$.alert.source");
12+
var ciName = gjp.read("$.alert.configuration_item");
13+
var ciSysId = gjp.read("$.alert.ci_sys_id");
14+
15+
// Validate that mandatory fields are present.
16+
if (!shortDescription || severity != 'CRITICAL') {
17+
response.setStatus(400); // Bad Request
18+
response.setBody({
19+
"status": "error",
20+
"message": "Missing mandatory alert information or severity is not critical."
21+
});
22+
return;
23+
}
24+
25+
// Use GlideRecordSecure for added security and ACL enforcement.
26+
var grIncident = new GlideRecordSecure('incident');
27+
grIncident.initialize();
28+
29+
// Set incident field values from the JSON payload.
30+
grIncident.setValue('short_description', 'INTEGRATION ALERT: [' + source + '] ' + shortDescription);
31+
grIncident.setValue('description', 'A critical alert has been received from ' + source + '.\n\nAlert Details:\nSeverity: ' + severity + '\nDescription: ' + shortDescription + '\nCI Name: ' + ciName);
32+
grIncident.setValue('source', source);
33+
grIncident.setValue('impact', 1); // Set Impact to '1 - High'
34+
grIncident.setValue('urgency', 1); // Set Urgency to '1 - High'
35+
grIncident.setValue('priority', 1); // Set Priority to '1 - Critical'
36+
37+
// If a CI sys_id is provided, set the Configuration Item.
38+
if (ciSysId) {
39+
grIncident.setValue('cmdb_ci', ciSysId);
40+
}
41+
42+
// Insert the new incident record and store its sys_id.
43+
var newIncidentSysId = grIncident.insert();
44+
45+
if (newIncidentSysId) {
46+
// Get the incident number for the successful response.
47+
var incNumber = grIncident.getRecord().getValue('number');
48+
49+
// Log the successful incident creation.
50+
gs.info('Critical P1 incident ' + incNumber + ' created from alert from ' + source);
51+
52+
// Prepare the success response.
53+
var responseBody = {
54+
"status": "success",
55+
"message": "Critical incident created successfully.",
56+
"incident_number": incNumber,
57+
"incident_sys_id": newIncidentSysId
58+
};
59+
response.setStatus(201); // Created
60+
response.setBody(responseBody);
61+
} else {
62+
// Handle database insertion failure.
63+
response.setStatus(500); // Internal Server Error
64+
response.setBody({
65+
"status": "error",
66+
"message": "Failed to create the incident record."
67+
});
68+
}
69+
70+
} catch (ex) {
71+
// Handle any exceptions during processing.
72+
gs.error('An error occurred during critical alert incident creation: ' + ex);
73+
response.setStatus(500);
74+
response.setBody({
75+
"status": "error",
76+
"message": "An internal server error occurred."
77+
});
78+
}

0 commit comments

Comments
 (0)