- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2025 04:50 AM
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() ?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2025 05:19 AM
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2025 04:55 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2025 04:58 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2025 05:02 AM
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?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2025 05:01 AM
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
-------------------------------------------------------------------------