Inbound Action - Script - Assign "Guest" if the from email does not match a user.

ditavaclavikova
Kilo Contributor

My script doesn't work. Can anyone help please. If a user that doesn't have ValueReatil in their email address sends an email to PeopleServ.... that is should open a case for them with "Guest" in opened for. 

 

However I get an error and no case gets opened.

 

Script attached in word or below:

 

gs.include('validators');
var flagticket = false;
var grWaterMark = new GlideRecord('sys_watermark');
var wVariable = email.body.ref;

// Check watermark for existing ticket and update it if found
if (wVariable!= undefined) {
grWaterMark.addQuery('number', wVariable);
grWaterMark.query();
if (grWaterMark.next()) {
var SrcID = grWaterMark.source_id.getDisplayValue();
var findTicket = new GlideRecord('sn_hr_core_case');
findTicket.addQuery('number', SrcID);
findTicket.query();
if (findTicket.next()) {

updateTicket(findTicket);
}
}
} else {

var tckstr = email.subject.toString();
var tckpos = tckstr.indexOf("HRC");
var tcknum = email.subject.substr(tckpos,10);
hrTicket = new GlideRecord('sn_hr_core_case');
hrTicket.addQuery('number', tcknum);
hrTicket.query();
while (hrTicket.next()) {
flagticket = true;
updateTicket(hrTicket);
}


}

// Create new HR Case if existing not found from watermark or subject
if (flagticket == false && wVariable == undefined) {
// Note: current.caller_id and current.opened_by are already set to the first UserID that matches the From: email address
var skipmsg = false;
var emailTo = email.direct;
//gs.log("email to value " + email.direct);
emailTo = emailTo.toLowerCase();
if ((emailTo.indexOf("peopleservices@valueretail.com") != -1)) {
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;
current.description = email.body_text;

profileCheck();

// current.opened_for = email.origemail;
// current.opened_by = email.origemail;
// current.subject_person = email.origemail;

}
}

// Core email rules assign "Guest" if the from email does not match a user.
// In this case, check the HR profile personal email, and reassign the case to that user.
function profileCheck() {
var profile;
if (gs.getUserID() == '5136503cc611227c0183e96598c4f706') {
profile = new GlideRecord('sn_hr_core_profile');
profile.addQuery('personal_email', email.origemail);
profile.query();
if (profile.next()) {
current.hr_profile = profile.sys_id;

if (profile.user) {
current.opened_for = profile.user;
current.opened_by = profile.user;
current.caller_id = profile.user;
}
}
} else {

// Find and attach profile if it exists
// current.caller_id = gs.getUserID();
// current.opened_by = gs.getUserID();
// current.opened_for = gs.getUserID();
current.caller_id = email.origemail;
current.opened_by = email.origemail;
current.opened_for = email.origemail;
profile = new GlideRecord('sn_hr_core_profile');
profile.addQuery('user', gs.getUserID());
profile.query();
if (profile.next()) {
current.hr_profile = profile.sys_id;
}

current.state = 10;
current.contact_type = "email";
current.priority = 3;
current.subject_person = current.opened_for;
current.insert();
}
}

function updateTicket(hrTicket) {

var rarray = email.recipients_array;

gs.include('validators');

if (hrTicket.getTableName() == "sn_hr_core_case") {
hrTicket.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;

if (email.subject.toLowerCase().indexOf("please reopen") >= 0) {
hrTicket.state = "2";
hrTicket.work_notes = "The caller did not feel that this case was resolved";
}

if (email.body.assign != undefined)
hrTicket.assigned_to = email.body.assign;

if (email.body.priority != undefined && isNumeric(email.body.priority))
hrTicket.priority = email.body.priority;

if (email.body.category != undefined)
hrTicket.category = email.body.category;

if (email.body.short_description != undefined)
hrTicket.short_description = email.body.short_description;



hrTicket.update();
}
}

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Just looking at the title of your thread, I did not read the whole code because it is a lot 🙂
You are after setting the assigned_to to the Guest user, if the email sender is unknown?

Have a look at below example. Tested and works on Incident.

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

	var assigneeSysId = '';
	
	var grUser = new GlideRecord('sys_user');
	if(!grUser.get('email', email.from)) {
		assigneeSysId = '5136503cc611227c0183e96598c4f706'; // Guest SysId
	}
	
	var grIncident = new GlideRecord('incident');
	grIncident.newRecord();
	grIncident.setValue('assigned_to', assigneeSysId);
	grIncident.insert();

})(current, event, email, logger, classifier);

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark, thanks for your response - I meant the Guest should be in "Opened for" - guess my header is confusing! Would you be able to help with that please?

Hi there,

Just a slight adjustment needed then.

var grUser = new GlideRecord('sys_user');
if(!grUser.get('email', email.from)) {
	current.setValue('opened_for', '5136503cc611227c0183e96598c4f706'); // Guest sys_id
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark, thanks so much, where would I place this?

Below is part of the script that's looking at the guest..

// Core email rules assign "Guest" if the from email does not match a user.
// In this case, check the HR profile personal email, and reassign the case to that user.
function profileCheck() {
var profile;
if (gs.getUserID() == '5136503cc611227c0183e96598c4f706') {
profile = new GlideRecord('sn_hr_core_profile');
profile.addQuery('personal_email', email.origemail);
profile.query();
if (profile.next()) {
current.hr_profile = profile.sys_id;

if (profile.user) {
current.opened_for = profile.user;
current.opened_by = profile.user;
current.caller_id = profile.user;
}
}
} else {

// Find and attach profile if it exists.....