Adding Users (Sys) to Watchlist on Inbound Action

Paul_Allen
Kilo Contributor

Hi

I am using Kingston and have tried a number of different scripts but none seem to work.

For NEW Email in (HR) I need an action that puts all of the CC people into the Watchlist Automatically, the only one so far that seems to work only adds their email address instead of their sys id which doesn't solve my issue

Essentially, if their email address exists then sys id, otherwise their email address.

Any takers?

1 ACCEPTED SOLUTION

Can you try this script

 

// Add All Email Recipients to Watch List
var wList = current.watch_list;
var gList = current.u_group_watch_list;
var rarray = email.recipients.toLowerCase().split(",");
var instanceEmail = gs.getProperty('glide.email.user');

for (var i = 0; i < rarray.length; i++) {
var recipient = rarray[i].toString();

//Check to see if this is a group address...and if so add to the group watch list
var grp = new GlideRecord('sys_user_group');
grp.addQuery('email', recipient);
grp.query();
if (grp.next()) {
if (gList != "") {
gList += "," + grp.getValue('sys_id');
} else {
gList = grp.getValue('sys_id'); //getting reset before
}
} else {

//It's not a group address...so check to see if it's a user
var gr = new GlideRecord('sys_user');
gr.addQuery('email', recipient);
gr.query();
if (gr.next()) {
// It's a user
if (wList != "") {
wList += "," + gr.getValue('sys_id');
} else {
wList = gr.getValue('sys_id');// getting reset before
}

} else {
//It's not a user either...so just add the address to the list...except instance email address
if (recipient != instanceEmail) {
if (wList != "") {
wList = wList + "," + recipient;
} else {
wList = recipient; //getting reset before
}
}
}
}
}
current.watch_list = wList;
current.u_group_watch_list = gList;
current.update();


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

8 REPLIES 8

Paul_Allen
Kilo Contributor

To help, here is the code that I'm trying (taken from another Community Case)

 

// Add All Email Recipients to Watch List


var wList = current.watch_list;


var gList = current.u_group_watch_list;


var rarray = email.recipients_array;


var instanceEmail = gs.getProperty('glide.email.user');

 


for (var i = 0; i < rarray.length; i++) {


var recipient = rarray[i];


//Check to see if this is a group address...and if so add to the group watch list


var grp = new GlideRecord('sys_user_group');


grp.addQuery('email', recipient);


grp.query();


if (grp.next()) {


if(gList != "") {


gList += "," + grp.getValue('sys_id');


} else {


gList = grp.getValue('sys_id');


}


} else {


//It's not a group address...so check to see if it's a user


var gr = new GlideRecord('sys_user');


gr.addQuery('email', recipient);


gr.query();


if (gr.next()) {


// It's a user


if(wList != "") {


wList +="," + gr.getValue('sys_id');


} else {


wList = gr.getValue('sys_id');


}


} else {


//It's not a user either...so just add the address to the list...except instance email address


if (recipient != instanceEmail) {


if(wList != "") {


wList = (wList + "," + recipient);


} else {


wList = recipient;


}


}


}

 

}

 

}

 


current.watch_list = wList;


current.u_group_watch_list = gList;


current.update();

Modified that code some.  I'm assuming you aren't using the group addresses/group watch list in this.  Also changed it to only look at the CC'd users.

// Add All Email Recipients to Watch List
var wList = []; 
wList = current.watch_list.split(',');

var rarray = email.copied.split(',');
var instanceEmail = gs.getProperty('glide.email.user');

for (var i = 0; i < rarray.length; i++) {
   var recipient = rarray[i];

   var gr = new GlideRecord('sys_user');
   gr.addQuery('email', recipient);
   gr.query();
   if (gr.next()) {
   		wList.push(gr.getValue('sys_id'));
   } else {
   		if (recipient != instanceEmail) {
   			wList.push(recipient);
   		}
 	}
}

current.watch_list = wList.join(',');

// Add the rest of your inbound action code

current.update();

Thanks Tim, I still can't seem to get it working, not sure if there is something in the current code that is blocking it - here is the existing code that I want to add it to - am I missing something obvious?

 

//Set all basic HR fields
current.incident_state = 1;
current.notify = 2;


if (email.importance !== undefined) {
if (email.importance.toLowerCase() == "high")
current.priority = 1;
} else
current.priority = 3;

var bodyText = email.body_text;
if (!bodyText)
bodyText = email.body_html;

//current.work_notes = "HR Case created by email:\n\nReceived from: " + email.origemail + "\n\n" + email.subject; //+ "\n\n" + bodyText;
current.description = "HR Case created by email:\n\nReceived from: " + email.origemail + "\n\nCopied to: " + email.copied + "\n\n" + bodyText;
current.u_initial_email_mag = "HR Case created by email:\n\nReceived from: " + email.origemail + "\n\nCopied to: " + email.copied + "\n\n" + email.body_html;
current.u_action_required = true;

// Core email rules assign "Guest" if the from email does not match a user.
// In this case, check the HR profile personal email, and reassing the case to that user.
var profile;
if (gs.getUserID() == '5136503cc611227c0183e96598c4f706') { //GUEST SYS_ID
profile = new GlideRecord('sn_hr_core_profile');
profile.addQuery('personal_email', email.origemail);
profile.query();
if (profile.next()) {
current.hr_profile = profile.sys_id;
current.opened_for = '';
current.caller_id = gs.getUserID();
current.opened_by = gs.getUserID();
if (profile.user) {
current.opened_for = profile.user;
current.opened_by = profile.user;
current.caller_id = profile.user;
}
} else {
current.caller_id = gs.getUserID();
current.opened_by = gs.getUserID();
current.opened_for = gs.getUserID();
}
} else {
// Find and attach profile if it exists
current.caller_id = gs.getUserID();
current.opened_by = gs.getUserID();
current.opened_for = gs.getUserID();
profile = new GlideRecord('sn_hr_core_profile');
profile.addQuery('user', gs.getUserID());
profile.query();
if (profile.next())
current.hr_profile = profile.sys_id;
}

current.subject_person = current.opened_for;
var newId = current.sys_id;
gs.eventQueue('sn_hr_core_case.email.creation', current, newId, email.from);

 

// Add All Email Recipients to Watch List
var wList = current.watch_list;
var gList = current.u_group_watch_list;
var rarray = email.recipients_array;
var instanceEmail = gs.getProperty('glide.email.user');

for (var i = 0; i < rarray.length; i++) {
var recipient = rarray[i].toString();

//Check to see if this is a group address...and if so add to the group watch list
var grp = new GlideRecord('sys_user_group');
grp.addQuery('email', recipient);
grp.query();
if (grp.next()) {
if (gList != "") {
gList += "," + grp.getValue('sys_id');
} else {
gList += grp.getValue('sys_id'); //getting reset before
}
} else {

//It's not a group address...so check to see if it's a user
var gr = new GlideRecord('sys_user');
gr.addQuery('email', recipient);
gr.query();
if (gr.next()) {


// It's a user
if (wList != "") {
wList += "," + gr.getValue('sys_id');
} else {
wList += gr.getValue('sys_id');// getting reset before
}

} else {
//It's not a user either...so just add the address to the list...except instance email address
if (recipient != instanceEmail) {
if (wList != "") {
wList = (wList + "," + recipient);
} else {
wList += recipient; //getting reset before
}
}
}
}
}
current.watch_list = wList;
current.u_group_watch_list = gList;
current.update();

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022