- 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-12-2025 12:44 PM
please try below code and let us know.
var gr = new GlideAggregate('tableName');
gr.addEncodedQuery('my query');
gr.groupBy('directory');
gr.query();
while(gr.next()){
var getRoles = new GlideRecord("roles_table_name");
getRoles.addQuery("directory", gr.getValue('directory'));
getRoles.query()
while(getRoles.next()){
gs.print(getRoles.getValue("roles"))
}
}
However, I believe we can do it using join query as mentioned below
Note : join query records works based on a relationship in a related table
var now_GR = new GlideRecord('roles');
now_GR.addJoinQuery('dictionary');
now_GR.query();
while(now_GR.next()){
gs.print(now_GR.getValue("role"))
}
To know more about Join Query, Please refer below docs link
If this helps, please mark this as correct/helpful.
Regards,
Krishnamohan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 10:47 PM
Hi @KrishnaMohan ,
Tries the script above, it prints blank and no output is displayed. Below is the script I tried:
var gr = new GlideAggregate('table name');
gr.addEncodedQuery('Query here');
gr.groupBy('directory');
gr.query();
while(gr.next()){
var getRoles = new GlideRecord("Role Table Name");
getRoles.addQuery("sys_id", gr.getValue('role'));
getRoles.query();
while(getRoles.next()){
gs.print(getRoles.getValue("role"))
}
}
Can you please assist
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 10:57 PM - edited 02-12-2025 10:59 PM
Also checked if I comment out group by line it prints the output with different roles available. But that is what not I am trying to achieve.
If there are similar directory name available as different rows in my M2M table then it should print different roles against the same directory available in 1 line and rest in other line.
Similar to screenshot I have attached above.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 01:36 PM - edited 02-12-2025 01:37 PM
You can try with simple script without array, split, push ...etc
*** Script: INC0000035- Reset my password
*** Script: INC0010004- test
*** Script: INC0010020- test
*** Script: INC0009001- test2
*** Script: INC0009005- test2