scripted rest api output creation

sherard
Mega Expert

New to using the Scripted REST API. I basically want to do a GET to the sys_user_grmember table to list the records of user within a group. However, using REST Explorer API a simple GET to the table provides sys_ids for the User and Group instead of the Names. So, without doing another REST call, I would like to create a scripted REST API that pulls the User Name and Group Name values for those sys_id from within the sys_user_grmember table.

I am having issues with trying to produce the Response Body output as I only return the last record. How would I create the Response Body output in a way that would be easily consumed?? I'm assuming I'm providing the Response Body output incorrectly.

Here is my script within the Scripted REST API:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

// implement resource here

var rec = {};

var gr = new GlideRecord("sys_user_grmember");

gr.addEncodedQuery('group.source=null');

gr.query();

while (gr.next()) {

        rec = {

                  "grmember_sysid": gr.sys_id,

                  "user": gr.user.name,

                  "user_sysid": gr.user.sys_id,

                  "group": gr.group.name,

                  "group_sysid": gr.group.sys_id

        };

}

return rec;

})(request, response);

Here is the Response Body output:

{
  "result": {
  "grmember_sysid": "fcd34380db51320087fdff561d96197f",
  "user": "Tiffany Knust",
  "user_sysid": "7d826bf03710200044e0bfc8bcbe5d26",
  "group": "CRM Engineering Team",
  "group_sysid": "68838b00db51320087fdff561d9619e1"
  }
}

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Try this instead:



(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {


// implement resource here



var output = [];


var gr = new GlideRecord("sys_user_grmember");


gr.addEncodedQuery('group.source=null');


gr.query();


while (gr.next()) {


        var rec = {


                  "grmember_sysid": gr.getValue('sys_id'),


                  "user": gr.user.name.toString(),


                  "user_sysid": gr.getValue('user'),


                  "group": gr.group.name.toString(),


                  "group_sysid": gr.getValue('group')


        };


        output.push(rec);


}


return output;


})(request, response);


View solution in original post

5 REPLIES 5

Hello, 

I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.

Could you please guide me how to implement this? Sample code would be great.

Input: Sys ID of the record 
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"sys_id": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"sys_id": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}