Query for unique values?

jkc
Giga Expert

I have the following code:

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading || newValue == '') {

  return;

  }

  DepList();

  }

function DepList(){

 

  var answerValue = [];

  alert("Department load startet.");

  var rec = new GlideRecord('u_masterdata_adress');

  rec.addQuery('u_country', g_form.getValue('country_selection'));

  //rec.addQuery('u_companyname', g_form.getValue('UserCompany'));

  rec.query();

  g_form.clearOptions('Choose_Department');

  g_form.addOption('Choose_Department', "-- None --", "-- None --", 0);

  g_form.addOption('Choose_Department', "-- My department is not in the list --", "-- My department is not in the list --", 1);

  g_form.setValue('Choose_Department', "-- None --", "-- None --");

  while (rec.next()){

  g_form.addOption('Choose_Department', rec.u_department, rec.u_department);

  }

  alert("DEPARTMENT LOAD COMPLETE");

}

And I would like to get a list of unique values only. Is this possible?

4 REPLIES 4

Harish Murikina
Tera Guru

Hi Jan,



                              It will helps you how can i add users from two different groups and check whether it contains duplicate entries ?



Regards,


Harish.


Anurag Tripathi
Mega Patron
Mega Patron

Hi Jan,



var arr =new ArrayUtil();


var uniqueMembers = [];


var rec = new GlideRecord('u_masterdata_adress');


  rec.addQuery('u_country', g_form.getValue('country_selection'));


  //rec.addQuery('u_companyname', g_form.getValue('UserCompany'));


  rec.query();



  while (rec.next()){


arr.push(rec.u_department);



}



unique_rec= arrayUtil.unique(arr);




This code will give you unique records in unique_rec array


-Anurag

Hi Jan,


I would make use of Anurag's code in a script include you call from your client script. ArrayUtil() is a handy server side util for dealing with arrays in ServiceNow and has the unique method, which is what you're looking for.



http://wiki.servicenow.com/index.php?title=ArrayUtil


santoshsahoonis
Kilo Guru

Hello,



First of all, don't use Glide objects in client scripts, instead use AJAX calls.



You can use GlideAggregate instead of GlideRecord and add a GroupBy query. This will return you unique values.



Do the following:


Step 1: create a scrip include (for AJAX calls)



Step 2: Assuming you know how to send parameters for an ajax call


send   "country_selection" and "UserCompany" values as parameters.


Check this : http://wiki.servicenow.com/index.php?title=GlideAjax




Step 3:



In the script Include, your callback function should contain the below code:




var country =this.getParameter(' '); // use the parameter name


var company =this.getParameter(' '); // use the parameter name



var rec = new GlideAggregate('u_masterdata_adress');


  rec.addQuery('u_country', country);


rec.groupBy('u_department');


  rec.query();


var dept_list = '';


while(rec.next()){


  if(dept_list == ''){


      dept_list += rec.u_department;


  }   else {


      dept_list += ','+rec.u_department;


  }


}


return dept_list;





Step 4:


parse the string(use split) in your client script and run a loop to populate the select box



Hope that helps !