Remove Duplicates from Watchlist.

kshaw
Giga Guru

Trying to refine a Business Rule that works on BEFORE insert or update.

On a catalog item I have the following:

  1.  Hiring Manager - single user reference variable
  2.  CCs - UI Macro with label variable that mimics WatchList  -  returns user sys_id
  3.  Interested Parties  - single text variable with comma separated list of emails.
  4. WatchList with previous additions from script or manually added by agents

What I need is a way to add the sys_ids/emails from items 1 to 3 to the existing watchlist but then do a check to ensure there are no duplicates before adding it into the record. Want to be able to dynamically deal with adding any new emails/users to these lists after original submissions.

Solution needs to be ES5 compliant - not ES6

Every example I have found and implemented just is not working and keeps producing errors.

find_real_file.png

// Get current watchlist
var currentWL = current.watch_list.toString().split(",");
    gs.log("starting watchlist: " + current.watch_list.toString())

// external email addrsses
var intParties = current.variable_pool.jobOpen_unit_intPartyEmail.split(","); 

// list of users in watchlist like macro variable
var cc = current.variable_pool.jobOpen_unit_cc.toString().split(","); 

// single person user
var hMgr = current.variables.jobOpen_unit_hireMgr.email; // single person user

var newEmails = intParties +"," + cc + "," + hMgr;
    gs.log ("Combined email list is " + newEmails);

var updatedWL = currentWL + "," + newEmails;
    gs.log ("Updated Watch List is " + updatedWL);
current.watch_list = updatedWL;

+++++++++++++++++++++++++++++++   CODE WORKS UP TO HERE ++++++++++++++++++++++++
// Remove duplicates
updatedWL = deduper = {};
updatedWL.forEach(function(item) {
deduper[item] = null;
var uniqueArray = Object.keys(deduper);
});

    gs.log ("Unique array is " + uniqueArray);

//current.watch_list = uniqueArray;

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron
Hi, I think you should be able to resolve your issues with snc's arrayutil 'unique' https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=c_ArrayUtilAPI

View solution in original post

2 REPLIES 2

Tony Chatfield1
Kilo Patron
Hi, I think you should be able to resolve your issues with snc's arrayutil 'unique' https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=c_ArrayUtilAPI

SatheeshKumar
Kilo Sage

Hi ,

You can try the below:

var currentWL = "test1@gmail.com,test2@gmail.com,test3@gmail.com";
var newEmails = "test1@gmail.com,test2@gmail.com,test7@gmail.com";


var updatedWL = currentWL + "," + newEmails;
gs.log ("Updated Watch List is " + updatedWL);



var emailArray = updatedWL.split(',');

var arrayUtil = new ArrayUtil();
//with duplicates
gs.log(emailArray);

var finalArray = arrayUtil.unique(emailArray);
//without duplicates
gs.log(finalArray.toString());



//current.watch_list = finalArray.toString();

 

 

-Satheesh