- 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 05:10 AM
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?
- 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 02:17 PM
Hi @Runjay Patel ,
I added the authentication method to the above code and it worked. Thanks for the help.