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

Omkar Kumbhar
Mega Sage

Hello @Utkarsha  ,

You can use below method and remove duplicates 

 

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').toString());

 

}

var au = new ArrayUtil();

 

var newChangeArr = au.unique(dupRecord);

 

},

type: 'SearchRec'

};

 

Your result will be stored in newChangeArr  variable.

 

Thank you,

Omkar

 

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

Hi Omkar,

Thank you for replying.

I tried the code you shared, still not able to remove duplicates, i need to call this script in report 
I am getting all the records from the table after running this filter

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
 

Hi @Anand Kumar P Sir,

The script is working like magic

Thank you so much 😊