Scripted REST API

Priyanka_Ghosh
Tera Contributor

Can we call/execute Scripted REST API from other server side scripts (eg: BR, Background script) like the way we execute REST API using var rest = new sn_ws RestMessageV2() ?

1 ACCEPTED SOLUTION

HI @Priyanka_Ghosh ,

 

Do like below.

// Create an instance of the RESTMessageV2 class
var request = new sn_ws.RESTMessageV2();

// Set the HTTP method and endpoint for your Scripted REST API
request.setHttpMethod('GET');  // or 'POST', 'PUT', etc.
request.setEndpoint('https://<instance_name>.service-now.com/api/now/v1/<your_scripted_api_endpoint>');

// You can add parameters or authentication if needed
// Example: request.setQueryParameter('sys_id', <sys_id>);

// Send the request
var response = request.execute(); 

// Capture the response from the API
var responseBody = response.getBody();
var responseCode = response.getStatusCode();

// Handle the response
if (responseCode == 200) {
    gs.info("Success: " + responseBody);
} else {
    gs.error("Failed: " + responseCode);
}

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

7 REPLIES 7

Hi @Runjay Patel , thanks for the response.

"Yes you can do, you can run from any server side script like BR, Script Include, UI action and other server script". - Can you let me know how can I do that?

HI @Priyanka_Ghosh ,

 

Do like below.

// Create an instance of the RESTMessageV2 class
var request = new sn_ws.RESTMessageV2();

// Set the HTTP method and endpoint for your Scripted REST API
request.setHttpMethod('GET');  // or 'POST', 'PUT', etc.
request.setEndpoint('https://<instance_name>.service-now.com/api/now/v1/<your_scripted_api_endpoint>');

// You can add parameters or authentication if needed
// Example: request.setQueryParameter('sys_id', <sys_id>);

// Send the request
var response = request.execute(); 

// Capture the response from the API
var responseBody = response.getBody();
var responseCode = response.getStatusCode();

// Handle the response
if (responseCode == 200) {
    gs.info("Success: " + responseBody);
} else {
    gs.error("Failed: " + responseCode);
}

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Runjay Patel ,

I added the authentication method to the above code and it worked. Thanks for the help.