|
| 1 | +Create Critical P1 Incident from Alert |
| 2 | +This script provides the server-side logic for a Scripted REST API endpoint in ServiceNow. It allows external monitoring tools to send alert data via a POST request, which is then used to automatically create a high-priority, P1 incident. |
| 3 | +Overview |
| 4 | +The API endpoint performs the following actions: |
| 5 | +Receives a JSON Payload: Accepts a POST request containing a JSON payload with alert details (severity, description, source, CI). |
| 6 | +Parses Data: Uses the GlideJsonPath API to efficiently extract the necessary alert information from the JSON body. |
| 7 | +Validates Request: Ensures that the severity is CRITICAL and the description is present. It sends an appropriate error response for invalid or incomplete data. |
| 8 | +Creates Incident: If the data is valid, it creates a new incident record in the incident table. |
| 9 | +Sets Incident Fields: Automatically populates the incident's short_description, description, source, and sets the impact, urgency, and priority to 1 - High/Critical. |
| 10 | +Associates CI: If a ci_sys_id is provided in the payload, it links the incident to the correct Configuration Item. |
| 11 | +Logs Activity: Logs the successful creation of the incident in the system log for tracking and auditing purposes. |
| 12 | +Responds to Sender: Sends a JSON response back to the external system, confirming success or failure and providing the new incident's number and sys_id. |
| 13 | +Expected JSON payload |
| 14 | +The external system should send a POST request with a JSON body structured like this: |
| 15 | +json |
| 16 | +{ |
| 17 | + "alert": { |
| 18 | + "severity": "CRITICAL", |
| 19 | + "description": "The primary database server is down. Users are unable to log in.", |
| 20 | + "source": "Dynatrace", |
| 21 | + "configuration_item": "DB_Server_01", |
| 22 | + "ci_sys_id": "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6" |
| 23 | + } |
| 24 | +} |
| 25 | +Use code with caution. |
| 26 | + |
| 27 | +Installation |
| 28 | +As a Scripted REST API Resource |
| 29 | +Create the Scripted REST API: |
| 30 | +Navigate to System Web Services > Scripted REST APIs. |
| 31 | +Click New and fill in the details: |
| 32 | +Name: CriticalAlertIncident |
| 33 | +API ID: critical_alert_incident |
| 34 | +Save the record. |
| 35 | + |
| 36 | + |
| 37 | +Create the Resource: |
| 38 | +On the Resources related list of the API record, click New. |
| 39 | +Name: PostCriticalIncident |
| 40 | +HTTP Method: POST |
| 41 | +Relative Path: / |
| 42 | +Copy and paste the provided script into the Script field. |
| 43 | +Configure Security: |
| 44 | +Ensure appropriate authentication is configured for the API, such as OAuth or Basic Auth, to secure the endpoint. |
| 45 | +Customization |
| 46 | +Change Priority/Impact: Modify the grIncident.setValue() lines to set different priority or impact levels based on the payload (e.g., if (severity == 'MAJOR') { grIncident.setValue('priority', 2); }). |
| 47 | +Add Additional Fields: Extend the script to parse and set other incident fields, such as assignment_group, caller_id, or category, based on data from the incoming payload. |
| 48 | +Enrich Incident Data: Perform a lookup on the CI to fetch additional information and add it to the incident description or other fields. |
| 49 | +Handle Different Severity Levels: Add if/else logic to handle different severity values (e.g., MAJOR, MINOR) from the source system, creating incidents with different priorities accordingly. |
| 50 | + |
| 51 | +Dependencies |
| 52 | +This script requires the GlideJsonPath API, which is available in Jakarta and later releases. |
| 53 | +The API endpoint must be secured with appropriate authentication to prevent unauthorized access. |
| 54 | + |
| 55 | +Considerations |
| 56 | + |
| 57 | +Security: This API endpoint is a powerful integration point. Ensure that it is properly secured and that only trusted sources are allowed to create incidents. |
| 58 | +Error Handling: The script includes robust error handling for common failures (missing data, insertion failure) but should be extended to handle specific use cases as needed. |
| 59 | +Testing: Thoroughly test the endpoint with a variety of payloads, including valid data, missing data, and invalid data, to ensure it behaves as expected. |
0 commit comments