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

Thanks, will give this a try!

-Rob

If that doesn't work than try just changing below on your 2nd script

current.u_additional_communication_list = aList.join(",");

Thanks, will give this a try!

-Rob

Hey Mike,

So, we got it to work! However, I seem to be missing something to make the emails for the CC'd - match the actual user ID in the system. The actual email address of the cc'd users are just showing as their email address.

Here is the code as I have it now, I am blanking on what I need to do to match it to the actual user.

//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;
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);

//Rob's Testing script Start
//add cc's to the Additional communication list

var aList = current.u_additional_communication_list;
var rarray = email.copied_array;
var instanceEmail = gs.getProperty('glide.email.user');

for (var i = 0; i < rarray.length; i++) {
	var copied = rarray[i];
	var gr = new GlideRecord('sys_user');
	gr.addQuery('email', copied);
	gr.query();
	if (gr.next()) {
		// It's a user
		if(aList != "") {
			aList = (aList + "," + gr.sys_id);
		} else {
			aList = gr.sys_id;
		}
	}
}
current.u_additional_communication_list = aList.join(",");
current.insert();
//Rob's Testing script End

Shows up on the form as:

find_real_file.png

Are you able to help me with what is needed to match the email to the actual user account? I tried copying the 'Find and attach profile if it exists' part of the script, but it did not work as I thought.

Thank you!

-Rob

I changed few thing so try that and check logs and see you are seeing logs.

var rarray = email.copied;
var instanceEmail = gs.getProperty('glide.email.user');

for (var i = 0; i < rarray.length; i++) {
	var copied = rarray[i];
	var gr = new GlideRecord('sys_user');
	gr.addQuery('email', copied);
	gr.query();
	if (gr.next()) {
		gs.log("It's a User: " + gr.name + " - " + copied, "inboundaction");
		// It's a user
		if(aList != "") {
			aList = (aList + "," + gr.sys_id);
		} else {
			aList = gr.sys_id;
		}
	}
}