Script to add cc'd users to watch list

mkader
Kilo Guru

Hello,

I need to create an inbound action that auto populates the watch list with  cc'd user(s) for RITMs. How can I do this? If a cc'd user does not exist in the system, I do not want to create a new user

Thanks!

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

Within this inbound action, in the script section, you can retrieve the "copied" users (aka CC'd) which will give you a comma separated string of their email address. From there, you can query your sys_user table to find a match based off of this and if found, add them to your watch list.

Something like:

var copied = email.copied;
var watchList = current.watch_list;
var findMem = new GlideRecord('sys_user');
findMem.addQuery('email', 'IN', copied);
findMem.addQuery('sys_id', 'NOT IN', watchList);
findMem.query();
while (findMem.next()) {
current.watch_list += "," + findMem.sys_id;
}
current.update();

I just tested this myself and it does work. So as long as your inbound action has found the correct record to associate the email to, this will work just fine.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

23 REPLIES 23

Ankur Bawiskar
Tera Patron
Tera Patron

@mkader 

Sample script for help

var ccUsers = email.copied;

var arr = [];

var rec = new GlideRecord('sys_user');
rec.addQuery('email', 'IN', ccUsers);
rec.query();
while(rec.next()){
arr.push(rec.getValue('sys_id'));
}

var existingWatchList = current.watch_list.toString().split(',');

var arrayUtil = new ArrayUtil();

var finalArr = arrayUtil.concat(existingWatchList,arr);

finalArr = arrayUtil.unique(finalArr);

current.watch_list = finalArr.toString();

current.update();

Regards
Ankur

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

@Ankur Bawiskar - Thank you for your response. I've tried this, and It does not seem to be triggering. I will set it up in my PDI and test again. Below screenshots is what I have done:

find_real_file.png

find_real_file.png

What is the best way to test this?

Hi,

Try like this.

var ccUsers = email.copied;
ccUsers = ccUsers.toString().split(",");
var users = [];
for(i=0;i<ccUsers.length;i++) {
  useres.push(ccUsers[i]);
}


var rec = new GlideRecord('sys_user');
rec.addQuery('email', 'IN', users);
rec.query();
while(rec.next()){
arr.push(rec.getValue('sys_id'));
}
gs.log("CC users in SN are "+arr);

var existingWatchList = current.watch_list.toString().split(',');

var arrayUtil = new ArrayUtil();

var finalArr = arrayUtil.concat(existingWatchList,arr);

finalArr = arrayUtil.unique(finalArr);

current.watch_list = finalArr.toString();

current.update();

@mkader 

send an email to the instance and keep some email address in cc.

remember to keep around 5 users in cc and out of those 5 only 3 should be present in ServiceNow.

Only 1 inbound email action would be processed for a table.

So keep the order as 10 and select the checkbox of Stop processing.

Regards
Ankur

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

I tried this in my PDI and was not able to make this work:

find_real_file.png

find_real_file.png

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

    var ccUsers = email.copied;
    ccUsers = ccUsers.toString().split(",");
    var users = [];
    for (i = 0; i < ccUsers.length; i++) {
        useres.push(ccUsers[i]);
    }


    var rec = new GlideRecord('sys_user');
    rec.addQuery('email', 'IN', users);
    rec.query();
    while (rec.next()) {
        arr.push(rec.getValue('sys_id'));
    }
    gs.log("CC users in SN are " + arr);

    var existingWatchList = current.watch_list.toString().split(',');

    var arrayUtil = new ArrayUtil();

    var finalArr = arrayUtil.concat(existingWatchList, arr);

    finalArr = arrayUtil.unique(finalArr);

    current.watch_list = finalArr.toString();

    current.update();

})(current, event, email, logger, classifier);

find_real_file.png