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

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka_Ghosh 

yes you can but ideally scripted rest api are used so that 3rd party or external system can call ServiceNow endpoint

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Priyanka_Ghosh
Tera Contributor

Hi @Ankur Bawiskar ,

thanks for the response. Could you please tell me HOW can we call/execute scripted rest APIs if it is the case?

 

@Priyanka_Ghosh 

but why to call?

Since you are invoking it from within the instance why not simply achieve the functionality with the same script i.e. BR or script include

syntax will be the same how you consume external REST API endpoint

what did you try so far and what didn't work?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Runjay Patel
Giga Sage

Hi @Priyanka_Ghosh ,

 

Yes you can do, you can run from any server side script like BR, Script Include, UI action and other server script.

 

Check this step by step guide for Scripted REST API.

https://servicenowwithrunjay.com/scripted-rest-api-servicenow/

 

 

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

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

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