Add CC users in watchlist without removing the users in watchlist already

Vikram3
Giga Guru

Hello All,

 

I'm using below script in inbound action to add the CC users in watchlist. It is working perfectly but it removes the users who are already in the watchlist.

 

 

try {
        var notWatchers = ['test@service-now.com'];
        var arr = email.recipients_array;
		var watchList = current.watch_list;
        var watchers = '';
        for (var i = 0; i < arr.length; i++) {
            if (notWatchers.indexOf(arr[i]) < 0) {
                if (arr[i] != email.from) {
                    var gr = new GlideRecord('sys_user');
                    gr.get('email', arr[i]);
                    if (gr.next()) {
                        watchers = watchers + ',' + gr.sys_id;
                    } else {
                        watchers = watchers + ',' + arr[i];
                    }
                }
            }

        }
        current.watch_list = watchers;
    } catch (e) {}

 

I wanted to append the CC users with the exisiting ones.

 

1 ACCEPTED SOLUTION

Ahmmed Ali
Mega Sage

Updated script below: Basically you can read current watchlist as array and then go on adding new values in same array and then assign that array value to watch list field.

 

try {
        var notWatchers = ['test@service-now.com'];
        var arr = email.recipients_array;
		var watchList = current.watch_list;
        var watchers = current.watch_list.toString().split(",");
        for (var i = 0; i < arr.length; i++) {
            if (notWatchers.indexOf(arr[i]) < 0) {
                if (arr[i] != email.from) {
                    var gr = new GlideRecord('sys_user');
                    gr.get('email', arr[i]);
                    if (gr.next()) {
                        watchers = watchers.push(gr.sys_id.toString());
                    } else {
                        watchers = watchers.push(arr[i] +"");
                    }
                }
            }

        }
        current.watch_list = watchers.join(",");
    } catch (e) {}

 

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

3 REPLIES 3

Ahmmed Ali
Mega Sage

Updated script below: Basically you can read current watchlist as array and then go on adding new values in same array and then assign that array value to watch list field.

 

try {
        var notWatchers = ['test@service-now.com'];
        var arr = email.recipients_array;
		var watchList = current.watch_list;
        var watchers = current.watch_list.toString().split(",");
        for (var i = 0; i < arr.length; i++) {
            if (notWatchers.indexOf(arr[i]) < 0) {
                if (arr[i] != email.from) {
                    var gr = new GlideRecord('sys_user');
                    gr.get('email', arr[i]);
                    if (gr.next()) {
                        watchers = watchers.push(gr.sys_id.toString());
                    } else {
                        watchers = watchers.push(arr[i] +"");
                    }
                }
            }

        }
        current.watch_list = watchers.join(",");
    } catch (e) {}

 

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hello @Ahmmed Ali It didn't work. Users are being removed from the watchlist.

Vikram3
Giga Guru

@Ahmmed Ali Actually it worked. I used my old code but I replaced the 4th line of yours which did the miracle. Thanks much.