How to add CC'ed users from inbound email to a custom field?

Rob Sestito
Mega Sage

Hello SN Comm!

I have seen many other posts trying to accomplish what I am asking for. However, none of the suggestions/answered posts has worked for me (it could be that I am putting the code in the wrong area of the script).

Nonetheless, Here is what I currently have for an Inbound Action to Create HR Case:

//Set all basic HR fields
if (email.importance !== undefined) {
	if (email.importance.toLowerCase() == "high")
		current.priority = 2;
} 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 = bodyText;

// 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.opened_by = gs.getUserID();
		if (profile.user) {
			current.opened_for = profile.user;
			current.opened_by = profile.user;
		}
	} else {
		current.opened_by = gs.getUserID();
		current.opened_for = gs.getUserID();
	}
} else {
	// Find and attach profile if it exists
	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);

Here is the script I am trying to play with/use: (my custom field is u_additional_communications_list - and it is very similar to watch list/collaborators field as it is a ref field to the sys_user table)

//populate additional communications list from cc filed

var aList = current.u_additional_communication_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];
	var gr = new GlideRecord('sys_user');
	gr.addQuery('email', recipient);
	gr.query();
	if (gr.next()) {
		// It's a user
		if(aList != "") {
			aList = (aList + "," + gr.sys_id);
		} else {
			aList = 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(aList != "") {
				aList = (aList + "," + recipient);
			} else {
				aList = recipient;
			}
		}
	}
	
}
current.u_additional_communication_list = aList;

This script is at the tail-end of the Inbound Action. Can someone lend a hand to see if we can get this to work correctly?

Thank you so much!

-Rob

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

You can leverage the ArrayUtil Script Include to simplify things:

var au = new ArrayUtil();
var instanceEmail = gs.getProperty("glide.email.user").toLowerCase().split(",");
var currentList = current.getValue("u_additional_communication_list").toLowerCase().split(",");

var copied = email.copied.toLowerCase().split();
//remove the instance email address
var copied = au.diff(copied, instanceEmail);

//get a list of User sys_ids and the matching email addresses
var ids = [];
var emails = [];
var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("emailIN" + copied.join(","));
gr.query();
while (gr.next()) {
	ids.push(gr.getValue("sys_id"));
	emails.push(gr.getValue("email").toLowerCase());
}

//remove email addresses we found from the copied list
copied = au.diff(copied, emails);

//now add the corresponding User record sys_ids
copied = au.union(ids, copied);

//now add to the existing list
current.u_additional_communication_list = au.union(currentList, copied).join(",");

View solution in original post

40 REPLIES 40

Rob Sestito
Mega Sage

Correct code for this post:

var au = new global.ArrayUtil();

if (email.importance !== undefined) {
	if (email.importance.toLowerCase() == "high")
		current.priority = 2;
} 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 = bodyText;
current.u_additional_communication_list = email.copied;

// 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.opened_by = gs.getUserID();
		if (profile.user) {
			current.opened_for = profile.user;
			current.opened_by = profile.user;
		}
	} else {
		current.opened_by = gs.getUserID();
		current.opened_for = gs.getUserID();
	}
} else {
	// Find and attach profile if it exists
	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);

//Testing script Start

var instanceEmail = gs.getProperty("glide.email.user").toLowerCase().split(",");
var currentList = current.getValue("u_additional_communication_list").toLowerCase().split(",");

var copied = email.copied.toLowerCase().split();
//remove the instance email address
var copied = au.diff(copied, instanceEmail);

//get a list of User sys_ids and the matching email addresses
var ids = [];
var emails = [];
var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("emailIN" + copied.join(","));
gr.query();
while (gr.next()) {
	ids.push(gr.getValue("sys_id"));
	emails.push(gr.getValue("email").toLowerCase());
}

//remove email addresses we found from the copied list
copied = au.diff(copied, emails);

//now add the corresponding User record sys_ids
//copied = au.union(ids, copied);

//now add to the existing list
current.u_additional_communication_list = au.union(ids).join(",");

//Testing script End

Does it work with email addresses not in ServiceNow?

No it does not.

Outside email addresses are never/would never be added to this Additional Communications List field.

This field was put together, to act similar to the Collaborators/Watch List fields, yet slightly different.

 

But - I ran into an "issue" anyway. When anyone from the CC'd list replies to the email. The system doesn't process the replies perfectly. While I want all those replies to go to the original Case that was created from the email they were CC'd on - the system creates a new Case.

So, it's not 100% as to how I would want it.

I only assume that I now need to modify the inbound action that controls replies.

Hi Rob,

Thanks for the code. The script does not seem to be adding the users on the  "to" line to the watchlist. It only adds "CC" users.Are you not currently adding users on to line to the watchlist?