Help with Inbound action - HR Case Update

Rob Sestito
Mega Sage

Hey SN Comm!

I have an ongoing project that I am trying to accomplish.

Last week I received help from our community with Inbound Action, Create HR Case. What I was trying to do, is have any CC's users that match a user account within SN sys_user table, to be added to a custom field 'u_additional_communications_list'. This field is very similar to Watch List and Collaborators List fields.

Here is the Code that works in doing the above:

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

//Rob's script Start
//match cc'd email address to user account profiles in servicenow and add their user account to additional communications list field

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); //commented out as this logic was not working

//now add to the existing list
current.u_additional_communication_list = au.union(ids).join(","); //adjusted this line to do the logic we want

//Rob's script End

 

Now, I believe I need to update the Inbound Action 'Update HR Case'. From testing the above, the thing I noticed is that when one of those CC's users reply to the original email - it will create a new case and only sometimes post to the original.

As you can see - I had each CC'd person reply, and it was not that consistent:

find_real_file.png

Here is the Update HR Case inbound actions script (for the core table):

gs.include('validators');
//gs.info("Update HR Case: Inbound Email - Entered Inbound Email Action");

if (current.transferred_to == "") {
	//gs.info("Update HR Case: Inbound Email - Not a transferred case");
	current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
	
	if (email.body.assign != undefined)
		current.assigned_to = email.body.assign;
	
	current.update();
} else {
	//gs.info("Update HR Case: Inbound Email - Transferred Case, updating tranferred case instead.");
	var gr = new GlideRecord("sn_hr_core_case");
	gr.get(current.transferred_to);
	gr.comments = "Comment forwarded from Transferred From Case:" + current.number + "\n\nreply from: " + email.origemail + "\n\n" + email.body_text;
	
	if (email.body.assign != undefined)
		gr.assigned_to = email.body.assign;
	
	gr.update();
}

 

Anyone able to lend a hand, as to what would be my best option in order to make all cc'd users that reply, have their reply go right to the Case that was already created?

Thanks in advance!

-Rob

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

You need to update your Create Inbound Action script to check, if there are existing requests with the same subject.

For ex, in my case, in create inbound action, I check, if there is an active security request with the same subject with or withour Re: .

Because when a new user sends a reply to that email, since there are no ServiceNow ref MSGID in the body or HR case number in the subject, ServiceNow will consider it as a new case.

var email_sub = email.subject.split(': ')[1];

var inc = new GlideRecord('sn_si_request');
//inc.addActiveQuery();
//inc.addQuery('short_description',email_sub);
inc.addEncodedQuery('active=true^short_description='+email_sub+'^ORshort_description='+email.subject);
inc.query();

if (inc.next())

{

// Add your update code here.

}


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

You need to update your Create Inbound Action script to check, if there are existing requests with the same subject.

For ex, in my case, in create inbound action, I check, if there is an active security request with the same subject with or withour Re: .

Because when a new user sends a reply to that email, since there are no ServiceNow ref MSGID in the body or HR case number in the subject, ServiceNow will consider it as a new case.

var email_sub = email.subject.split(': ')[1];

var inc = new GlideRecord('sn_si_request');
//inc.addActiveQuery();
//inc.addQuery('short_description',email_sub);
inc.addEncodedQuery('active=true^short_description='+email_sub+'^ORshort_description='+email.subject);
inc.query();

if (inc.next())

{

// Add your update code here.

}


Please mark this response as correct or helpful if it assisted you with your question.

Thanks for explaining this Sanjiv - as it worked perfectly for me!

Thank you!

-Rob

Hi Sanjiv,

 

Could you please let us know how to check the smart allocators for HR cases.

If possible, please give quick replay.

 

Thanks and Regards.