How to group by an attribute and print values using background script

Surbhi Srivasta
Tera Expert

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:

 

SurbhiSrivasta_2-1739387564881.png

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

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Surbhi Srivasta 

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

AnkurBawiskar_0-1739433246799.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

13 REPLIES 13

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 

@Surbhi Srivasta 

Request you to close this thread by marking appropriate response as correct.

Discussion can continue on the new thread.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Surbhi Srivasta 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar - I have marked your answer as correct as that was helpful and worked for me. Sorry for late response here