Help with Inbound Email Action script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2020 02:01 PM
We have an Inbound Action set to take information from an incoming email (from a form) that is forwarded into ServiceNow. There is a script in the inbound action that does lots of different things. One of the things it does is searches for a known user based on the origemail address. Once it recognizes the user, it creates the Caller (caller_id) with the known user record. If not a known user, it uses a Guest profile and populates an alternate contact info field (string) and puts the guest user's email address (found in the body of email from form input) in the Watch List.
The form's functioning is about to change, and so will the requirements for this inbound action. With the change, the origemail (From email) will always be the same (an internal email address to avoid spam and other issues). So, now the user will never be recognized, because it isn't the user's email address. So, I'd like the script to look for the email address from the body instead of using origemail. Same with the watch list - instead of populating based on origemail, use the email address from the body.
I've tried lots and lots of different things to make this happen and I can't get it to work.
** Here's the section of the script that searches the sys_user and sets caller:
// - Check to see if this is a Library Services inbound email.
if (email.subject.startsWith("GKR:")||email.subject.startsWith("GIL:")||email.subject.startsWith("DLG:")||email.subject.startsWith("DLGSysadmin")||email.subject.startsWith("NGE:")||email.subject.startsWith("GALILEO:")) {
var target = new GlideRecord('sys_user');
target.addQuery('email', 'CONTAINS', email.origemail);
target.query();
// - If the user Does Not exist
if (!target.hasNext()) {
// - Create the Incident against the Library Guest user
current.caller_id = '4de6da154fd072007c553d728110c7ae';
** Here's the section of the script that populates the watch list:
else{
// Not GALILEO email
// Note: current.opened_by is already set to the first UserID that matches the From: email address
current.caller_id = gs.getUserID();
if (gs.getUserID() == '5136503cc611227c0183e96598c4f706') { //GUEST SYS_ID set watchlist
current.u_customer_watch_list = email.origemail;
}
}
**Here's the body of the email snipped to show where I need to pull the email address for the caller search and to set the watch list:
<SNIP>
Ticket Type: GALILEO
Comment ID: GALILEO:2020-06-12T11:16:00-04:00
Date: 2020-06-12 11:16:00 -0400
First Name: Test User
Email Address: test.user@usg.edu
Phone: 000-000-0000
<SNIP>
**Here is my latest attempt that did not work:
var target = new GlideRecord('sys_user');
target.addQuery('email', 'CONTAINS', email.body_text.EmailAddress); //user record email found in body
target.query();
// - If the user Does Not exist
if (!target.hasNext()) {
// - Create the Incident against the Library Guest user
current.caller_id = '4de6da154fd072007c553d728110c7ae';
I've tried camel case, I've tried email.body.emailAddress, I've tried setting a variable, other things I can't remember. There are other parts of the script that set fields based on the body that work fine. Not sure why I can't set the Caller and customer Watch List with information from the body.
Any advice is greatly appreciated.
Thanks!
- Labels:
-
Incident Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2020 02:47 PM
Hi
I just wanted to check-in on this and see how things were going.
If my reply above helped guide you correctly, please mark as Correct.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2020 06:12 AM
Hello Allen A, sorry for the long delay since your reply. I've had to put this issue on hold for awhile but have resumed.
OK - so I got the right combination of punctuation (thanks!) and got the parts of the script working that set the watch list.
I thought it was correctly searching sys_user table and finding known users based on the email address in the body, but more recent testing is making me question that. The caller_id is being set to "Guest" for known users, I think that is happening, based on the Opened_by, which is still populating based on From: email because of highlighted portion of the script below, so I'm resuming my work on that, assuming if I can change that I'll be in business. This part of the script looks to be copied from the default inbound action (inactive in our instance).
I am encouraged that target.addQuery('email', 'CONTAINS', email.body.email_address); might be working or the caller_id would be set to Library Guest and not Guest, right?
// - Check to see if this is a Library Services inbound email.
if (email.subject.startsWith("GKR:")||email.subject.startsWith("GIL:")||email.subject.startsWith("DLG:")||email.subject.startsWith("DLGSysadmin")||email.subject.startsWith("NGE:")||email.subject.startsWith("GALILEO:")) {
var target = new GlideRecord('sys_user');
target.addQuery('email', 'CONTAINS', email.body.email_address);
target.query();
// - If the user Does Not exist
if (!target.hasNext()) {
// - Create the Incident against the Library Guest user
current.caller_id = '4de6da154fd072007c553d728110c7ae';
<SNIP>
} else {
// Note: current.opened_by is already set to the first UserID that matches the From: email address
current.caller_id = gs.getUserID();
}
//keep no-reply or noreply out of customer watch list
if (email.origemail.includes("no-reply")||email.origemail.includes("noreply")){
current.u_customer_watch_list = '';
}
else{
current.u_customer_watch_list = email.body.email_address;
Not really sure what to put in its place, but I've just started back on it. Any suggestions would be greatly appreciated. I am thinking that
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2020 02:21 PM
The functionality you are looking for, to set the key:value pairs requires a lowercase variable name:
7 Setting Field Values from the Email Body
Values in an inbound email can set field values in a task record. Any name:value pair in an inbound email body gets parsed into a variable/value pair in the inbound email script. The name:value pair must be on its own line. Note that most email clients limit the number of characters allowed per line and may truncate excessively long name:value pairs. To populate a reference field use setDisplayValue() instead. See Redirecting Emails for an example of using setDisplayValue() in an inbound email action.
Note: The action always generates a lowercase variable name. Note also that this functionality does not work on reference fields. |
Edit: There are other ways to achieve this with custom code. I would personally rely on those myself since I control it. My fear would be this method would somehow be taken away or something and few would notice, including yourself. Because of that I would parse out of the body myself.