How to get a list of unique values of a field of a partiular table

surya123
Mega Guru

How to get a list of unique values of a field of a partiular table

1 ACCEPTED SOLUTION

Hi Surya,



Something like this would do it. I would recommend caution when running queries on task as the table can be very large and could cause performance issues.



var gr = new GlideAggregate("task");


gr.addAggregate("COUNT");


gr.groupBy("cmdb_ci");


gr.query();


while (gr.next()) {


      gs.print(gr.getAggregate("COUNT") + " -- " + gr.getValue("cmdb_ci") + " -- " + gr.getDisplayValue("cmdb_ci"));


}



Does this help?


View solution in original post

7 REPLIES 7

Hi Surya,



Something like this would do it. I would recommend caution when running queries on task as the table can be very large and could cause performance issues.



var gr = new GlideAggregate("task");


gr.addAggregate("COUNT");


gr.groupBy("cmdb_ci");


gr.query();


while (gr.next()) {


      gs.print(gr.getAggregate("COUNT") + " -- " + gr.getValue("cmdb_ci") + " -- " + gr.getDisplayValue("cmdb_ci"));


}



Does this help?


Hi Richard,


Is there a way I can get the list as comma separated values.


You'll need to build it up yourself while iterating over the GlideRecord.



I would use an push each item onto an array and then join the array. Something like this:


var idArr = [];



var gr = new GlideAggregate("task");


gr.addNotNullQuery("cmdb_ci");


gr.addAggregate("COUNT");


gr.groupBy("cmdb_ci");


gr.query();


while (gr.next()) {


      idArr.push(gr.getValue("cmdb_ci"));


}



gs.print(idArr.join(","));



In line 12 I use the join function which returns a String representation of the Array delimited by a comma.



Thanks,



Cameron