Create Scripted REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago
HI All,
I have a requirement to create a Scripted REST api to generate a JSON response from ServiceNow by combining cmdb_ci_service_auto and service_offering and business application table and I need to pick 3-4 fields from each table and send through JSON response.
AM new to this completely. How to achieve this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
Hello @Aswin Chandras1 ,
For this navigate to Scripted rest services > Scripted REST APIs :
> Create a new one - define <name>
> In resources create a record with GET method
Use this script as per your requirement :
var result = [];
var grServiceAuto = new GlideRecord('cmdb_ci_service_auto');
grServiceAuto.query();
while (grServiceAuto.next()) {
var serviceObj = {
service_name: grServiceAuto.getValue('name'),
service_id: grServiceAuto.getUniqueValue(),
status: grServiceAuto.getValue('status'),
owner: grServiceAuto.getDisplayValue('owned_by')
};
var grOffering = new GlideRecord('service_offering');
grOffering.addQuery('service', grServiceAuto.sys_id);
grOffering.query();
if (grOffering.next()) {
serviceObj.offering_name = grOffering.getValue('name');
serviceObj.offering_id = grOffering.getUniqueValue();
serviceObj.offering_status = grOffering.getValue('status');
}
var grApp = new GlideRecord('cmdb_ci_business_app');
grApp.addQuery('used_for', grServiceAuto.sys_id);
grApp.query();
if (grApp.next()) {
serviceObj.app_name = grApp.getValue('name');
serviceObj.app_id = grApp.getUniqueValue();
serviceObj.app_owner = grApp.getDisplayValue('owned_by');
}
result.push(serviceObj);
}
// Write JSON response
response.setBody(result);
After configuring all you will get an endpoint like this known as resource path -
which you can test to get an output.
if my response helped mark as helpful and accept the solution.