Target Table of Received Email Inconsistent / Incorrect

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2024 05:02 AM - edited 12-14-2024 05:43 AM
Hello. I'm trying to setup Inbound Email Actions for HR Case records.
I noticed that when I compose (from scratch) an arbitrary email to our ServiceNow instance, and I include "RE: CaseNumber" in the subject line, the target table will be identified (poorly) as sn_hr_core_case, even though the Case Number is for a more specific (child table) case such as sn_hr_core_case_payroll.
But when I actually reply to an email from our ServiceNow instance, the instance correctly identifies the target table such as sn_hr_core_case_payroll. It somehow does this even if I change the subject line and I strip out the watermark. (I guess it's somehow using hidden email header information to do this).
Question 1 - Is composing a brand new email to a ServiceNow instance with subject line "RE CaseNumber" a valid scenario? Should the instance be correctly identifying the specific table of the record? Or does this seem like a ServiceNow bug?
Question 2 - If I want an Inbound Email Action that handles 5 different case types:
sn_hr_core_case
sn_hr_core_case_benefits
sn_hr_core_case_payroll
sn_hr_core_case_operations
sn_hr_core_case_talent_management
Do I really need to create 5 separate inbound email actions that all do exactly the same thing? Or is there a way to have just one inbound email action handle all 5 case types, since they all derive from sn_hr_core_case?
Screenshot showing how ServiceNow sometimes (poorly) identifies received emails with the (parent) table instead of the specific child table:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2024 01:33 PM - edited 12-14-2024 01:37 PM
(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
// Extract the case number from the email subject using a regex pattern
// The regex looks for 'HRC' followed by exactly 7 digits
var caseNumberMatch = email.subject.match(/HRC\d{7}/);
if (caseNumberMatch) {
// If a case number is found, extract the matched value
var caseNumber = caseNumberMatch[0];
// Create a GlideRecord object for the parent table 'sn_hr_core_case'
var grHRC = new GlideRecord('sn_hr_core_case');
grHRC.addQuery('number', caseNumber);
grHRC.query();
if (grHRC.next()) {
// If a record is found in the parent table, retrieve its class name
// This determines the specific child table (e.g., 'sn_hr_core_case_payroll')
var childTableClass = grHRC.getRecordClassName();
// Dynamically create a GlideRecord object for the child table
var childHRRecord = new GlideRecord(childTableClass);
childHRRecord.get(grHRC.sys_id); // Retrieve the record from the child table
if (childHRRecord.isValidRecord()) {
// Perform different actions based on the child table type
switch (childTableClass) {
case 'sn_hr_core_case_payroll':
// Actions for payroll cases
childHRRecord.comments = "Payroll case: " + email.body_text;
break;
case 'sn_hr_core_case_benefits':
// Actions for benefits cases
childHRRecord.comments = "Benefits case: " + email.body_text;
break;
case 'sn_hr_core_case_operations':
// Actions for operations cases
childHRRecord.comments = "Operations case: " + email.body_text;
break;
case 'sn_hr_core_case_talent_management':
// Actions for talent management cases
childHRRecord.comments = "Talent management case: " + email.body_text;
break;
default:
// Default action, if record is a record just existing on the parent table
//(if sn_hr_core_case is the specific table) or other child tables
childHRRecord.comments = "Default action: " + email.body_text;
break;
}
// Update the child record after applying the specific action
childHRRecord.update();
}
} else {
// Log an error if no record with the case number is found in the parent table
gs.info('No case found with this case number in the parent table.');
}
} else {
// Log a message if no valid case number is found in the email subject
gs.info('No valid case number found in the email subject.');
}
})(current, event, email, logger, classifier);
---
If this saved your day, hit that like button and make my day 😊
If it works, accept this solution or you will fail in your next promotion^^
If you need further support, just give me a shout!
Cheers