use inbound email action to add email found in body to watch list

Fraggle Rock
Tera Expert

hi there have a use case where we recive a MS form into ServiceNow 

 

in the body of the email is the email of the person submitting the form ( note the person is  not in our user table they are a guest)

 

\We want to get the grab the email from the body and add them to the watchlist. when the form comes to ServiceNow the field in the email will always come in the the below format.

 

ReqEmail: bob@bob.com

 

and we are on Vancouver 

3 REPLIES 3

Srinivasa1
Tera Expert

Hi @Fraggle Rock ,

 

May be try something like this in the actions part:

var wList = current.watch_list;

wList = wList.split(",");

var inputText = String(email.body_text);

// Define the regular expression pattern to match email addresses
var emailPattern = /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;

 

// Find all matches in the input text
var matches = inputText.match(emailPattern);

// Check if matches were found
if (matches) {
// Loop through matches and add them to the array
for (var i = 0; i < matches.length; i++) {
wList.push(matches[i]);
}
}


current.watch_list = String(wList);

thanks for that i'll give it a go later.

for my POC i have gone simple with 

 

// set guest email
    var user_contact = email.body.email;
    current.watch_list = user_contact;
 
which is working but the email address on the email that i receive has brackets around it that i want to strip eg: email: [atest@test.com]

 

Sumanth16
Kilo Patron

Hi @Fraggle Rock ,

 

 

// 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 = (gList + "," + grp.sys_id);

  } else {

  gList = grp.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 = (wList + "," + gr.sys_id);

  } else {

  wList = gr.sys_id;

  }

  } else {

  //It's not a user either...so just add the address to the list...except the 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();

 

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

 

Thanks & Regards,

Sumanth Meda