Scripted REST API Response returned as "Undefined"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 10:43 PM
Hi Folks,
Have created a scripted rest API which takes in a company sys_id as input and is supposed to return all the assignment group names and member count (for the respective groups) tagged under the company. But currently I am getting back no responses once I test it through the API explorer.
Below is the Get resource code I have implemented.
function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var clientID = request.pathParams.client_id;
gs.log('client id:' + clientID);
var writer = response.getStreamWriter();
var cnt1 = 0;
var hdrs = {};
hdrs['Content-Type'] = 'application/json';
response.setStatus(200);
response.setHeaders(hdrs);
//start building the JSON string
writer.writeString("{\"results\":[");
//query comapny table for sys_id validation
var comp = new GlideRecord('core_company');
comp.addQuery('sys_id','=',clientID);
comp.query();
cnt1 = comp.getRowCount();
gs.log("company count:" + cnt1);
if(cnt1== 0){
writer.writeString("Error: Invalid Client ID");
}
else{
writer.writeString(global.JSON.stringfy("valid Client ID"));
var grp = new GlideRecord('sys_user_group');
grp.addQuery('company','=',clientID);
//grp.addActiveQuery();
grp.query();
var cnt2 = grp.getRowCount();
gs.log('group count:' + cnt2);
if(cnt2==0){
writer.writeString(global.JSON.stringfy("No group is associated with the specific client"));
}
else{
while(grp.next()){
var groupObj = {};
var grpm = new GlideAggregate('sys_user_grmember');
grpm.addQuery('group',grp.sys_id);
grpm.addAggregate('COUNT');
grpm.query();
var grpMembers = 0;
if(grpm.next()){
grpMembers = grpm.getAggregate('COUNT');
}
groupObj.groupName = grp.name + '';
groupObj.groupID = grp.sys_id + '';
groupObj.groupMembers = grpMembers;
writer.writeString(global.JSON.stringfy(groupObj));
if(grp.hasNext()){
writer.writeString(",");
}
}
}
//close the response object
writer.writeString("]}");
}
})(request, response);
Let me know if someone also faced something similar.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 10:59 PM
Hi Rohit,
First of all few corrections and check
comp.addQuery('sys_id',clientID);
grp.addQuery('company',clientID);
are you getting cnt1 and cnt2 value ?
add log statement and debug 1 by 1
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 06:03 AM
Hi Ankur,
Thanks for the response. I am getting the expected values for cnt1 and cnt2 and without and with the corrections suggested. Though still don't see the response coming up.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 06:29 AM
Hi Rohit,
Did you add log statements to check what value is printed for object groupObj just before pushing it into writer
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2019 04:32 AM
Yes Ankur. All the counters are getting properly updated on the logs. Have added log statement for displaying the groupname as well before pushing it to the writer and am getting the group names. My guess that the issue has something to do the data passing to the writer