Remove inactive users from watchlist

ValeriaS
Tera Contributor

I am trying to make a scheduled job that runs once a day. The purpose of this scheduled job is to look into all active incidents which contain at least one person in the watchlist and remove any inactive users from the watchlist. Can anyone help with this?

1 ACCEPTED SOLUTION

Uncle Rob
Kilo Patron

Ok, I built this out for the practice. Before we start, please be aware that Glide_List fields are simply strings that store comma separated sys_ids.  Observe.

UncleRob_3-1732840983015.png

You'll also need to define two flow variables
- NewWatchList:  this will store our valid user sys_id's to pump back into the watch list
- RemovedNames:  This will store a string of friendly user names that we removed from the watch list.  This isn't required, just going to make your Incident work notes more informative.

 

Here's the flow....

UncleRob_10-1732841893317.png

 

My first look up was just to a single sample Incident.  You'd want to look up a set of records then do a For Each to loop through them.  Anyway... here's what my sample incident started with.  Akila P is inactive

UncleRob_2-1732840920279.png

My first For Each is simple. Just a For Each entry in the incident's Watch List.  

UncleRob_5-1732841151492.png

Since the For Each is getting a user reference each time, lets ask if that user is active.

UncleRob_6-1732841244495.png

If the user IS active, lets pump their sys_id to flow variable NewWatchList, which is a string that will contain comma separated sys_ids.  Don't copy this verbatim as the code will change based on the node you're pulling from and yours might not match mine.  If you've never scripted values in flow you type in fd_data. and the system helps you fill in the rest.  Its how you access your Data Picker via script.  Super noice.
This code says "combine the NewWatchList variable with whatever its previous value was, plus a comma, plus the sys_id of the user I'm currently on in the For Each.

UncleRob_7-1732841327761.png

So if the user is NOT active, lets add their name to flow variable RemovedNames.  Notice this time I'm not getting the user's sys_id, I'm getting their name!

UncleRob_8-1732841529457.png

Now that we've looped through all the users in the watch list, lets update the Incident.  But we only need to update IF there were users to remove.  We'll know if the cleanse was unnecessary if RemovedNames is not empty.

UncleRob_11-1732841988528.png

Now we just update the watch list (with NewWatchList) and then add a charming little work note to inform people which users have been removed from the watch list.

 

UncleRob_9-1732841656450.png

 

BOOM!!   Cleansed watch list and informative work note if users were removed.  (forgive me I was too lazy to hyper optimize the grammar of the work note update.)

UncleRob_12-1732842107018.png

 

 

 

 

View solution in original post

12 REPLIES 12

Uncle Rob
Kilo Patron

Ok, I built this out for the practice. Before we start, please be aware that Glide_List fields are simply strings that store comma separated sys_ids.  Observe.

UncleRob_3-1732840983015.png

You'll also need to define two flow variables
- NewWatchList:  this will store our valid user sys_id's to pump back into the watch list
- RemovedNames:  This will store a string of friendly user names that we removed from the watch list.  This isn't required, just going to make your Incident work notes more informative.

 

Here's the flow....

UncleRob_10-1732841893317.png

 

My first look up was just to a single sample Incident.  You'd want to look up a set of records then do a For Each to loop through them.  Anyway... here's what my sample incident started with.  Akila P is inactive

UncleRob_2-1732840920279.png

My first For Each is simple. Just a For Each entry in the incident's Watch List.  

UncleRob_5-1732841151492.png

Since the For Each is getting a user reference each time, lets ask if that user is active.

UncleRob_6-1732841244495.png

If the user IS active, lets pump their sys_id to flow variable NewWatchList, which is a string that will contain comma separated sys_ids.  Don't copy this verbatim as the code will change based on the node you're pulling from and yours might not match mine.  If you've never scripted values in flow you type in fd_data. and the system helps you fill in the rest.  Its how you access your Data Picker via script.  Super noice.
This code says "combine the NewWatchList variable with whatever its previous value was, plus a comma, plus the sys_id of the user I'm currently on in the For Each.

UncleRob_7-1732841327761.png

So if the user is NOT active, lets add their name to flow variable RemovedNames.  Notice this time I'm not getting the user's sys_id, I'm getting their name!

UncleRob_8-1732841529457.png

Now that we've looped through all the users in the watch list, lets update the Incident.  But we only need to update IF there were users to remove.  We'll know if the cleanse was unnecessary if RemovedNames is not empty.

UncleRob_11-1732841988528.png

Now we just update the watch list (with NewWatchList) and then add a charming little work note to inform people which users have been removed from the watch list.

 

UncleRob_9-1732841656450.png

 

BOOM!!   Cleansed watch list and informative work note if users were removed.  (forgive me I was too lazy to hyper optimize the grammar of the work note update.)

UncleRob_12-1732842107018.png

 

 

 

 

This worked, thank you very much

Luiz Lucena
Mega Sage

Hi @ValeriaS and @Uncle Rob 

We did something very easy, we just added a condition to the Dictionary Entry for the watch list:
Just add a condition like the one below and any task where the Watch List is populate by a user that becomes inactive, they will be automatically removed. 

Screenshot 2024-11-28 at 20.43.04.png

Started digging into this @Luiz Lucena because I suspected the reference qualifier would block selection, but leave existing inactive user entries in-tact.  Here's what I found.

Reference qualifier definitely blocks entry.  Here's my user, akila p.  As you can see... inactive.

UncleRob_0-1732884983187.png

Here's a brand new incident, attempting to add akila to the watch list.

UncleRob_1-1732885024912.png

But here's an older incident with akila already on the watch list.  She still remains (and is visible) on the interface.

UncleRob_2-1732885064811.png

So just letting you know, legacy entries of inactive users are still there if you're using reference qualifier only.  I'll bet that's not a problem 99% of the time, but data is data, and if you were running any scripts or logic... that won't care if the user is active or not.

hello @Luiz Lucena, I did this already but as @Uncle Rob  if there was an incident which had the user already and then that user becomes inactive, it does not remove the user. I have been trying to achieve this with an scheduled job. This is what I have so far:

var incidentGR = new GlideRecord('incident');
incidentGR.addEncodedQuery('active=true^watch_listISNOTEMPTY');
incidentGR.query();

while (incidentGR.next()) {
    var watchList = incidentGR.watch_list;
    var watchListArray = watchList.split(','); // Converting to an array
    var updatedWatchListArray = [];
    var workNotesMessage = ''
 
    for (var i = 0; i < watchListArray.length; i++) {
        var userSysId = watchListArray[i].trim();

        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userSysId) && userGR.active == 'false') {
            updatedWatchListArray = updatedWatchListArray.filter(function(sysId) {
                return sysId !== userSysId;
            });
           
            // Add a message to work notes for the removed user
            workNotesMessage += userGR.name + " has been removed from the watchlist because they are inactive. ";
        } else {
            updatedWatchListArray.push(userSysId);
        }
    }
 
    var updatedWatchList = updatedWatchListArray.join(',');

    if (updatedWatchList != watchList) {
        incidentGR.watch_list = updatedWatchList;

        // If there was a change to the watch list, set work notes
        if (workNotesMessage) {
            incidentGR.work_notes = workNotesMessage;
        }

        incidentGR.update();
    }
}

Not sure what I might be doing incorrectly, can any of you advise?