Create Scripted REST API

Aswin Chandras1
Tera Contributor

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.

1 REPLY 1

yashkamde
Mega Sage

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.

Screenshot 2026-06-16 215526.png

 

if my response helped mark as helpful and accept the solution.