Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get Unique Values?

Utkarsha
Tera Contributor

Hello Experts,

I need to create a report based on a scripted filter, based on reference field for one of the tables, I'm using below script- 

var SearchRec = Class.create();
SearchRec.prototype = {
initialize: function() {},
findDup: function() {
var dupRecord = [];
var vdrRecords = new GlideAggregate('sn_vdr_tpro_checker');
vdrRecords.addEncodedQuery('u_draft!=cancelled');
vdrRecords.addAggregate('COUNT', 'u_local_party');
vdrRecords.orderBy('u_local_party');
vdrRecords.addHaving('COUNT', '>', 1);
vdrRecords.query();
while (vdrRecords.next()) {
dupRecord.push(vdrRecords.getValue('u_local_party'));

}
return dupRecord;
},



type: 'SearchRec'
};

Could anyone please guide me here, I want only single unique records for each of the u_local_party field the table, currently when we are applying group by using 'u_local_party' we are getting multiple records for single 'u_local_party' field value.
All suggestions are welcome
Thank you
1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron

Hi @Utkarsha  ,

 

 

var SearchRec = Class.create();
SearchRec.prototype = {
  initialize: function() {},
  findUniqueRecords: function() {
    var uniqueRecordSysIDs = [];
    var previousLocalParty = '';
    var vdrRecords = new GlideRecord('sn_vdr_tpro_checker');
    vdrRecords.addEncodedQuery('u_draft!=cancelled');
    vdrRecords.orderBy('u_local_party');
    vdrRecords.query();
    while (vdrRecords.next()) {
      var localParty = vdrRecords.u_local_party.toString();
      if (localParty !== previousLocalParty) {
        uniqueRecordSysIDs.push(vdrRecords.sys_id.toString());
        previousLocalParty = localParty;
      }
    }

    return uniqueRecordSysIDs;
  },

  type: 'SearchRec'
};

 

 


Try above script.
Please mark it as solution proposed and helpful it serves your purpose.
Thanks,

Anand
 

View solution in original post

5 REPLIES 5

Your welcome @Utkarsha

Please mark it as helpful.

Thanks,

Anand