Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Add Email to Watch List directly from Inbound Email

mwascak
Kilo Contributor

We've using our Service-Now instance for external customer support and need to be able to have non-users emails populate the Watch List field directly from an allowed inbound customer email.

Anyone have any experience with doing this without adding them as a user?

7 REPLIES 7

Not applicable

I have having error with this script. It works if the watchlist is empty but once the watchlist has information inside it will fail.

i think it is because of this statement:
var recipient = rarray;
for (var o = 0; o < wListSplit.length; o++) {
if(current.watch_list==wListSplit[o]){ // error at this line
wAdd = false;
}}


I believe it should be recipient==wListSplit[o].

Also, in the current.watch_list store sys_id, so, I am not sure if it will match it correctly.

Feedback will be good.

Thanks.


Not applicable

I have done improvement to the script and tested.

// Add All Email Recipients to Watch List
var wList = current.watch_list;
var wListSplit = wList.split(',');
var wAdd = true;
var rarray = email.recipients_array;
var instanceEmail = gs.getProperty('glide.email.user');

for (var i = 0; i < rarray.length; i++)
{
var recipient = rarray;
for (var o = 0; o < wListSplit.length; o++)
{
if(recipient==wListSplit[o])
{
wAdd = false;
}
}

if(wAdd == true)
{
var wAdd2 = true;
var gr = new GlideRecord('sys_user');
gr.addQuery('email', recipient);
gr.query();
if (gr.next())
{

for (var o = 0; o < wListSplit.length; o++)
{
if(gr.sys_id==wListSplit[o])
{
wAdd2 = false;
}
}

// It's a user
if(wAdd2 == true)
{
wList = (wList + "," + gr.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;


mwascak
Kilo Contributor

This works as well and adds logging to debug

// Add All Email Recipients to Watch List
gs.log("*** Create Unique Incident Script Executing ***");
var wList = current.watch_list;
var gList = current.u_group_watch_list;
var rarray = email.recipients_array;
var instanceEmail = gs.getProperty('glide.email.user');
var usrEmail = email.origemail;
gs.log(" *** User Email =: " + usrEmail);

var usr = new GlideRecord('sys_user');
usr.addQuery('email', usrEmail);
usr.query();
if (usr.next()) {
gs.log("*** Address has User associated: " + usr.sys_id + " " + usr.name);
// Its a user
current.caller_id = usr.sys_id;
current.opened_by = usr.sys_id;
} else {
gs.log("*** Address DOES NOT have User associated: " + usrEmail );
current.u_external_customer = "true";
wList = usrEmail;
//Might want to generic user to populate open by EMAIL
}

for (var i = 0; i < rarray.length; i++) {
var recipient = rarray;
gs.log("Current recipient: " + recipient);
var gr = new GlideRecord('sys_user');
gr.addQuery('email', recipient);
gr.query();
if (gr.next()) {
if (recipient != instanceEmail) {
gs.log("*** Recipient is not Instance Email");
gs.log("*** Adding SysId to Watch List");
wList = (wList + "," + gr.sys_id);
}
else {
gs.log("*** Adding Recipient to Watch List" + recipient);
wList = (wList + "," + recipient);
}
}
}