Removing duplicate values from an array in service now

Shantharao
Kilo Sage

How to remove duplicate values from an array.

1. I am getting values from two groups

2. there few users are common for both the groups

3. I want to remove duplicate users and I need to add them into watch list field on the incident form

Below is the script and attached document

 

(function executeRule(current, previous /*null when async*/) {
var groupName = "CAB Approval";
var groupName1 = "Capacity Mgmt";
var answer = [];
var mem = new GlideRecord('sys_user_grmember');
mem.addQuery('group.name', groupName1).addOrCondition('group.name', groupName);
mem.query();
while (mem.next()) {
answer.push(mem.user.name.toString());
gs.addInfoMessage("answer is " + answer);
//current.watch_list = answer.join(',');
// **remove duplicates from an array**
var au = new ArrayUtil();
var uniqueArrayElements = au.unique(answer);
gs.addInfoMessage("unique values" + uniqueArrayElements);
current.watch_list = uniqueArrayElements.join(',');
}
})(current, previous);

 

Thanks

4 REPLIES 4

Harsh Vardhan
Giga Patron

try now. 

 

(function executeRule(current, previous /*null when async*/) {
	var groupName = "CAB Approval";
	var groupName1 = "Capacity Mgmt";
	var answer = [];
	var mem = new GlideRecord('sys_user_grmember');
	mem.addQuery('group.name', groupName1).addOrCondition('group.name', groupName);
	mem.query();
	while (mem.next()) {
		answer.push(mem.user.name.toString());

	}




	var unique = {};
	answer.forEach(function(i) {
		if(!unique[i]) {
			unique[i] = true;
		}
	});
	gs.log('unique key is '+Object.keys(unique));
	current.watch_list= Object.keys(unique).toString();


})(current, previous);

 

If my answer helped you , kindly mark it as correct and close this thread. 

Lukasz Bojara
Kilo Sage

Hi,

You can use the filter() method of an array. It will check the the condition and return new array with duplicates removed from the initial array. Please see an example below:

var array = ['One','Two','Three','Four','Two','Three','Three'];


// item is the current item from an array
// position is the index of current item
// arr is the array that is beeing processed

var test = array.filter(function(item, position, arr){

	return arr.indexOf(item) == position; 

  /* indexOf will return a first index of the value in the array 
     and will compare it to the current index, if they will not match
     this item will not be included in the new array
  */

});

 

Best regards,
Łukasz

Sebastian R_
Kilo Sage

Hi Shantharao,

the most efficient way for your use case would be the following. First you store the sys_id of the member in an object (automatic unique values). Later you get an array of the sys_id with Object.keys

var answer = {};
var mem = new GlideRecord('sys_user_grmember');
mem.addQuery('group.name', groupName1).addOrCondition('group.name', groupName);
mem.query();
while (mem.next()) {
answer[mem.getValue('user')] = true;

}

current.watch_list = Object.keys(answer).split(',');

Dubz
Mega Sage

Using the ArrayUtil method as you have done is the best way to do this, you just need to do it outside of the while loop. you'll also need to pass in the user sys_id not the name:

(function executeRule(current, previous /*null when async*/) {
var groupName = "CAB Approval";
var groupName1 = "Capacity Mgmt";
var answer = [];
var mem = new GlideRecord('sys_user_grmember');
mem.addQuery('group.name', groupName1).addOrCondition('group.name', groupName);
mem.query();
while (mem.next()) {
answer.push(mem.getValue('user'));
}
gs.addInfoMessage("answer is " + answer);
//current.watch_list = answer.join(',');
// **remove duplicates from an array**
var au = new ArrayUtil();
var uniqueArrayElements = au.unique(answer);
gs.addInfoMessage("unique values" + uniqueArrayElements);
current.watch_list = uniqueArrayElements.join(',');
})(current, previous);