How to get Unique records from table for a choice field

vamsi
Tera Contributor

I have a table 'xyz' which contains redundant values suppose like name

xyz

Name         company

----------     -------
John             Google

Mark           Microsoft

John             Google

when I give choice   table as xyz and choice field as 'Name' im getting John,Mark,John but I need only one John.

like John,Mark

Is there any way to get it without changing xyz table dictionary (in wiki we have unique id concept but that is field dictionary but   I need redundant values in 'xyz' table).

1 ACCEPTED SOLUTION

bernyalvarado
Mega Sage

Hi Vamsi, just in case it helps, there's various post that recommend using an array, yet, i believe a hash is the best way to go in terms of performance to get unique values. Below I add an example where I collect all the unique states from the incidents table:



var myStatesHash = {};


gr = new GlideRecord('incident');


gr.query();


while (gr.next())


{


  myStatesHash[gr.state] = gr.state.getDisplayValue();


}



var state = '';


for (state in myStatesHash) {


  gs.print(state + ' : ' + myStatesHash[state]);


}



Just in case you want to look to the array options:


pull only distinct values - This is a good one for you as well since it does the same thing you're trying to accomplish


Also, there's an arrayUtil which has a .unique function: http://wiki.servicenow.com/index.php?title=ArrayUtil#gsc.tab=0



I hope this is helpful! Cheers!



Thanks,
Berny


View solution in original post

5 REPLIES 5

PeterWiles
Kilo Sage

You will need to create a script include and use a dynamic reference qualifier.



http://wiki.servicenow.com/index.php?title=K12_Advanced_Scripting#Script_Include_-_Lab_2&gsc.tab=0



Pete


bernyalvarado
Mega Sage

Hi Vamsi, just in case it helps, there's various post that recommend using an array, yet, i believe a hash is the best way to go in terms of performance to get unique values. Below I add an example where I collect all the unique states from the incidents table:



var myStatesHash = {};


gr = new GlideRecord('incident');


gr.query();


while (gr.next())


{


  myStatesHash[gr.state] = gr.state.getDisplayValue();


}



var state = '';


for (state in myStatesHash) {


  gs.print(state + ' : ' + myStatesHash[state]);


}



Just in case you want to look to the array options:


pull only distinct values - This is a good one for you as well since it does the same thing you're trying to accomplish


Also, there's an arrayUtil which has a .unique function: http://wiki.servicenow.com/index.php?title=ArrayUtil#gsc.tab=0



I hope this is helpful! Cheers!



Thanks,
Berny


Thank you so much Berny



It resolved my issue.


Awesome!!