Reference Qualifier: How to get unique records in a reference field for a table which has duplicate values

ashwanikumar
Tera Expert

Hi All,

i am trying to get unique records from a table which has duplicate records. I am able to achieve it by my script but it is taking a lot of time to execute. Following is my exact requirement:

1. Custom table has duplicate records for the users with the user ID.

2. I need to return recently created unique record for each user.

e.g. following table should return Record #2 for User A, #4 for user B and #5 for user C.

#
User Name
user id
Created (Date)
1User A0000127/02/2017
2User A0000127/03/2017
3User A0000127/01/2017
4User B0000227/03/2017
5User C0000327/02/2017
6User C0000327/01/2017

Following is my script:

//Glide the table , order by the user id and run query.

  var resource = new GlideRecord('table_name');

  resource.orderBy('user_id');

  resource.query();

  while (resource.next()){

//Glide again and add one more condition to filter out only those records which matches with resource user id. descending order by created and pust the recently created record

  var resource2 = new GlideRecord('table_name');

  resource2.addQuery('user_id', resource.user_id);

  resource2.orderByDesc('sys_created_on');

  resource2.query();

  if(resource2.next()){

  sysid += (',' + resource2.sys_id);

  }

  }

 

As I am gliding 2 times to get   the unique records and also gliding for all the duplicate records, script is taking a lot of time.

Please let me know what is the best approach to write reference qualifier for this case?

Thanks,

Kumar

2 REPLIES 2

Jaspal Singh
Mega Patron
Mega Patron

Hi Ashwani,



Did you try looking How to set the default search column on a list view   link.


venkatiyer1
Giga Guru

Hi Ashwani,



Use GlideAggregate instead.