How to call API through Business rule?

Kanna12
Tera Expert

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!

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

https://www.servicenow.com/community/developer-forum/access-external-api-from-business-rule/m-p/1506...

A Youtube video

https://www.youtube.com/watch?v=RiuMvyA3jfw

Ravi Chandra_K
Kilo Patron
Kilo Patron

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.

https://www.servicenow.com/community/developer-forum/how-invoke-rest-api-from-business-rule/m-p/2082...

 

 

Please Mark this answer as helpful and correct if helped.

Kind Regards,

Ravi

Amit Verma
Kilo Patron
Kilo Patron

Hi @Kanna12 

 

You can create a business rule as shown below :

AmitVerma_0-1729495105679.png

 

AmitVerma_2-1729495221857.png

 

(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.

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

HitoshiOzawa_0-1729552871384.png

HitoshiOzawa_1-1729552887668.png

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);