Removing duplicate values from an array in service now
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2019 12:26 AM
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
- Labels:
-
Field Service Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2019 12:51 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2019 12:52 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2019 12:55 AM
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(',');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2019 01:33 AM
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);