Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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);


Dang I was so close...



Thanks!


One additional question....



Is it better to create this as an array as you listed or one big object? or does it matter to the consumer?



I would think this be a specific Object structure, no?


Because you are getting multiple attributes per record, it makes more sense (to me) to have an array of objects. Each array element represents one record.