Introduction
π Webhooks are a powerful way to enable real-time integrations between ServiceNow
and external systems. π Whether you're sending incident updates π or triggering external workflows βοΈ, webhooks help streamline automation π€.
In this guide, we'll walk through setting up a webhook in ServiceNow step by step π οΈ, covering everything from writing the script βοΈ to configuring the event registry π and business rule π.
By the end, you'll have a fully functional webhook β and the debugging skills π΅οΈββοΈ to troubleshoot issues effectively. π
π₯ TL;DR
Whenever a new operation (Create π / Update βοΈ / Delete β / Query π) is performed on the Incidents table, weβll:
1οΈβ£ Create a Business Rule π that executes after the operation finishes (to grab the most up-to-date data).
2οΈβ£ Dispatch an Event π with two parameters:
- Current Data π¦ (which is needed in theory but doesn't work well for extracting data).
- sys_id π (a unique identifier for each record).
3οΈβ£ Write an Event Script π» that:
- Uses sys_id to query the Incidents table.
- Fetches the latest data π .
- Sends it to an external consumer π.
By the end, weβll have a real-time data synchronization β‘ and a fully functional webhook for external integrations! ππ
1οΈβ£ Registering the Event in the Event Registry
We need first create an event.
Steps:
- Navigate to Plataform Analytics Administration > Data Collector > Event Registry.
- Click New to create a new event.
- Set Table to
Incident [incident]
. - Add a description like Triggers a webhook when an incident is created or updated.
- Click Submit to save the event.
2οΈβ£ Creating the Script Action to Send an HTTP Request
The core of our webhook is a Script Action, which will send an outbound HTTP request when triggered. Let's start by writing the script that will handle this operation.
Steps:
- Navigate to System Policy > Events > Script Actions.
- Click New to create a new script.
- Set the Event name to match the one we'll register in the step above (e.g.,
incident.webhook
). - Check the Active checkbox
- Use the following script to send an HTTP request:
(function executeWebhook(event) {
try {
const consumerEndpoint = "https://YOUR_ENDPOINT";
gs.info("π₯ Outbound Message");
const sysId = event.parm1;
var incidentTableRecord = new GlideRecord("incident");
incidentTableRecord.get(sysId);
incidentTableRecord.query();
if (incidentTableRecord.next()) {
const recordSysId = incidentTableRecord.getValue("sys_id");
const shortDescription = incidentTableRecord.getValue("short_description");
const description = incidentTableRecord.getValue("description")
const date = incidentTableRecord.getValue("sys_created_on");
const state = incidentTableRecord.getValue("state");
const incidentId = incidentTableRecord.getValue("number")
const priority = incidentTableRecord.getValue("priority");
var requestBody = {
shortDescription,
recordSysId,
date,
state,
incidentId,
description,
priority
};
gs.info("Sending Webhook Payload: " + JSON.stringify(requestBody));
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setEndpoint(consumerEndpoint);
restMessage.setHttpMethod('POST');
restMessage.setRequestHeader('Content-Type', 'application/json');
restMessage.setRequestBody(JSON.stringify(requestBody));
var response = restMessage.execute();
var responseBody = response.getBody();
gs.info('β
Webhook response: ' + responseBody);
} else {
gs.info("β No incidents found.");
}
} catch (error) {
gs.error('Error sending webhook: ' + error.message);
}
})(event);
- Click Submit to save the Script Action.
3οΈβ£ Creating a Business Rule to Trigger the Webhook
With the event in place, we need a Business Rule to fire it when an incident is created or updated.
Steps:
- Navigate to Activity Subscriptions > Adminsitration > Business Rules.
- Click New.
- Set Table to
Incident
. - Set When to
After
- Check Insert and Update checkboxes
- In the Advanced section, add the following script, updating it the event name created.
(function executeRule(current, previous /*null when async*/) {
gs.info("Executing Business Rule");
gs.eventQueue("your.event.here", current, current.sys_id); // UPDATE YOUR EVENT HERE
})(current, previous);
- Click Submit to save the Business Rule.
4οΈβ£Β Time to create an incident π
Use REST API Explorer
Debugging and Troubleshooting
- Check the System Logs π₯
If your webhook isn't working, go to System Logs > All and search for:
Verify the Event is Triggering
Use REST API Explorer
Conclusion π
By following these steps, you've successfully created a webhook in ServiceNow using a Business Rule, Event Registry, and Script Action. This setup enables real-time integration with external services and can be customized further based on your needs. π
Let me know if you have any questions or need enhancements! Happy coding! π¨βπ»π₯
Top comments (0)