How to call API through Business rule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2024 10:44 PM
Hello Every one,
I am new to SN teschical side.
I have a requirement: I need to monitor the users table, any departmnet manager changed I want to call API to update some external application.
I am wiring the script in business rule to monitor the managers field values in users table, if anything values is changed
I need to call API.
Can you provide me how to accomplish this requirement?
Inputs are more helpful!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2024 10:59 PM - edited 10-20-2024 11:29 PM
So where are you getting stuck?
Just create an async business rule to execute on Update.
Check the "Advanced" checkbox and write your script to call external application in the Script section.
EDIT: Following thread has a sample script to call external api from business rule.
A Youtube video
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2024 11:24 PM
Hello @Kanna12
You can use sn_ws.RestMessageV2 method to call an end point from Business Rule.
Please find below thread where the process is clearly explained.
Please Mark this answer as helpful and correct if helped.
Kind Regards,
Ravi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 12:21 AM
Hi @Kanna12
You can create a business rule as shown below :
(function executeRule(current, previous /*null when async*/ ) {
try {
var r = new sn_ws.RESTMessageV2('Yahoo Finance', 'get'); // Replace with your REST Message
var response = r.executeAsync();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
}
})(current, previous);
Please replace the Rest Message name with your Rest Message and the required method.
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 04:22 PM
Complete example.
1. Since you're calling an external api to update some record, it's recommended to use Async business rule instead of After business rule because After will wait for the api call to be finished thus hanging up your servicenow instance. Exception is when you need to post the previous value. When using async, previous value will be null so you'll need to use after business rule.
2. SInce you're updating, you need to make a POST call or a PUT call to send a data to the external system.
Code:
(function executeRule(current, previous /*null when async*/ ) {
try {
// Prepare the RESTMessageV2 object
var request = new sn_ws.RESTMessageV2('Firebase Cloud Messaging Send', 'post');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);
// Set request headers if necessary
request.setRequestHeader('Accept', 'application/json');
request.setRequestHeader('Content-Type', 'application/json');
// Construct the request body
var requestBody = {
// Add your data here
manager: current.manager.getDisplayValue(),
// more fields as required
};
// Set the request body
request.setRequestBody(JSON.stringify(requestBody));
// Send the request
var response = r.execute();
// Handle the response
var httpResponseStatus = response.getStatusCode();
var responseBody = response.getBody();
var parsedResponse = JSON.parse(responseBody);
// Logging for debugging purposes
gs.info('HTTP Response Status: ' + httpResponseStatus);
gs.info('Response Body: ' + responseBody);
// Do something based on the response
if (httpResponseStatus === 200) {
// Success logic
} else {
// Error handling
}
} catch (error) {
gs.error('Error in calling external service: ' + error.message);
}
}
})(current, previous);