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

That's Okay Brian-

Yes they are - we house all of our employees in SN, something around 53k. So anyone being CC'd will most likely be an employee (active or not).

Going to check it out more, and see if what Mike said helps me.

-Rob

I would take a look at what Jim provided.  It looks like it would work based on the code he provided.

Hey Brian - I did try what Jim provided, however it did not work. I replied to him with the warnings I received. Still just the emails were populated and not the actual user's user ID.

Thanks,

-Rob

Rob Sestito
Mega Sage

I am still unable to get this to work correctly.

you need to something like on your 1st script

current.subject_person = current.opened_for;

current.u_additional_communication_list = email.copied;