- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 11:16 AM
Hi,
I have a table where I have 2 columns named Directory and Roles. Now Directory can be same on multiple records, I need help to print out the value of all Roles for the same directory in 1 single line.
For example, this is how my table is structured:
There are 2 D1 which are having 2 different Roles R1 and R3. Similarly for D2, there are 2 roles R2 and R4
Output:
R1,R3
R2,R4
I have tried below script so far to group by using glide aggregate but this is not working. Both Directory and Roles are reference attributes
var gr = new GlideAggregate('tableName');
gr.addEncodedQuery('my query');
gr.groupBy('directory');
//gr.setGroup(false);
gr.query();
while(gr.next()){
gs.info(gr.getValue('roles'));
}
Kindly assist on how can i achieve the above output.
Regards,
Surbhi
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 11:54 PM
try this
var gaUser = new GlideAggregate('tableName');
gaUser.addAggregate('count');
gaUser.orderByAggregate('count');
gaUser.groupBy('directoryField');
gaUser.addEncodedQuery('my query');
gaUser.query();
while(gaUser.next()){
var roleCount = gaUser.getAggregate('count');
gs.info('Directory: ' + gaUser.directoryField + ' has roles : ' + roleCount);
}
I used something similar on sys_user and it worked for me
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 08:55 PM
Yes, started doing that with thought process being below:
1. Form an array.
2.Form an JSON object for both attributes.
3.Push this JSON object into an array
4.Try to find similar keys which are available within an array and then have comma separated values obtained which was my initial requirement.
var arr = [];
var getRoles = new GlideRecord('Table Name');
getRoles.addEncodedQuery('my query');
getRoles.query();
while(getRoles.next()){
var obj = {};
obj.directory = getRoles.getValue('directory'),
obj.roles = getRoles.getValue('roles');
arr.push(obj);
}
gs.info(JSON.stringify(arr)); //Give me array of JSON objects
for(var i=0;i<arr.length;i++){
gs.info(JSON.stringify(arr[i]));
}
This is what I have tried so far and stuck on point number 4, can you help me how can we extract this - I have created a separate post related to this extraction piece to share details rather than mixing it up - https://www.servicenow.com/community/developer-forum/how-to-get-value-from-array-of-json-objects/m-p...
Am I in right direction or there is simpler way to do this @Ankur Bawiskar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 09:25 PM
Request you to close this thread by marking appropriate response as correct.
Discussion can continue on the new thread.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2025 07:06 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2025 03:06 AM
@Ankur Bawiskar - I have marked your answer as correct as that was helpful and worked for me. Sorry for late response here