Inbound Action - Need to set "Opened by" to the "From" header in the body of a forwarded email

Sysop
Tera Contributor

I'm forwarding an email to our SN instance and it's creating a request with the "Opened by" set to my name.  Is it possible for me to set it to set it to John Smith from the header information here in the email body?  Also can I set it to where if John Smith isn't a user in our ServiceNow instance that it will then default to me, the sender?

 

find_real_file.png

 

Right now I'm using this in the script of my Inbound Action

 

current.opened_by = gs.getUserID();
current.short_description = email.subject;
current.description= email.body_text;
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;

1 REPLY 1

Allen Andreas
Administrator
Administrator

Hi,

You'd need to query your sys_user table using the email.origemail (which would be the original sender) to find a possible match, then if not found, use you...so script would be something like:

var u;
var gr = new GlideRecord('sys_user');
gr.addQuery('email', email.origemail);
gr.query();
if (gr.next()) {
u = gr.getValue('sys_id');
} else {
u = gs.getUserID();
}
current.opened_by = u;
current.short_description = email.subject;
current.description= email.body_text;
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;

There are times where email.origemail isn't pulling the original sender, if that's the case, then you'll have to adjust the script with something like:

var u;
var str = email.body.from;
var subStr = str.substring(str.lastIndexOf("<") + 1,str.lastIndexOf(">"));
var gr = new GlideRecord('sys_user');
gr.addQuery('email', subStr);
gr.query();
if (gr.next()) {
u = gr.getValue('sys_id');
} else {
u = gs.getUserID();
}
current.opened_by = u;
current.short_description = email.subject;
current.description= email.body_text;
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!