Add users to watchlist

Mishu
Tera Expert

In my custom application, I have a choice field as 'Type' with around 10-15 choice options, of which 3 values are also group names. When any of these 3 values are selected then members of the group should be added to the watchlist and if changed to another choice value then the members should be removed.
But if any other user is manually added to the watchlist along with the group members it should be retained on saving the form along with other members until the type gets changed.

 

I am able to achieve the group members gettting added to watchlist on type selection, but when adding a user manually to watchlist with other group members, the user is not getting saved..it bring back to the just the group members only.

Can somebody please help me in modifying it in my below code :

 

On Before Insert/Update BR :

 
(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    //current.internal_watchlist = "";
    var members = [];
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group.name''Artist Royalties Audit Request Catalog');
    gr.addQuery('user.active'true);
    gr.query();
    while (gr.next()) {
        members.push(gr.getValue('user'));
    }
    var new_memb = '';

    if(current.internal_watchlist.toString().length > 0)

        new_memb = current.internal_watchlist.toString() + ',' + members.join();
    else
        new_memb = members.join();
    current.internal_watchlist = new_memb;

})(current, previous);
3 REPLIES 3

Mishu
Tera Expert

@Ankur Bawiskar -- Can you please guide here ?

Ankur Bawiskar
Tera Patron
Tera Patron

@Mishu 

where is the logic for removal?

what is the BR condition for your BR? where are you check the choice values?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar ---

 

I wrote a different BR(before update) for removing the users with below conditions:

 

Type changes from Group 1 or Group2 or Group3

 

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
current.internal_watchlist = "";

if(current.internal_watchlist != ""){
var wt_list = current.getValue('internal_watchlist');
current.internal_watchlist = wt_list;

}})(current, previous);
 
what is the BR condition for your BR?  -- For the BR mentioned in my question initially the condition was Type is Group1/Group2/Group3 or Changes to Group1...I wrote 3 separate BR with same logic for all the 3 groups
 
 
where are you check the choice values? only 3 Choices are group name, others are normal choice values