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

Brian Lancaster
Tera Sage

Please take a look here.  I believe what you are looking for is email.copied.

Thanks for replying Brian.

Unfortunately, from your suggested link, I don't see how this tells me how I can make my inbound email action add CC'd users automatically to a field like 'Watch List' (replaced however by my custom field).

Thanks,

-Rob

I think I see what this means now.

Sorry I have not been seeing replies in my community inbox.  Is the CC'd user a user in your ServiceNow environment?