Target Table of Received Email Inconsistent / Incorrect

G24
Kilo Sage

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:

ReceivedEmailIssue.png

 

1 REPLY 1

Justin Donath
Tera Contributor
Hey G24!
 
If you write an arbitrary email as a reply to an existing case, you probably have no watermark on this mail.
In this case, Servicenow searches the subject and body for a record number.*1

JustinDonath_0-1734211474714.png*2
 
According to your screenshot, it finds the correct record, but sets the parent table instead of the specified child table. I could not find the logic for setting the target table based on watermark or record number, 
so I do not know why it is working for watermark and not for record number.
I could imagine that in case of considering the record number, ServiceNow is 

1. checking the prefix of the record (in your case "HRC"), 
2. searching then for the record via GlideRecord on the corresponding base table (in your case "sn_hr_core_case")
3. finding the correct record, but setting the target_table to previously identified base table instead of setting it via gr.getRecordClassName() (which would return the correct (child) table)
 
Since I don't want to accuse Servicenow of such a simple error, I would recommend you to escalate the problem you are facing to Servicenow.
 
So to answer your first question:
Yes, "RE CaseNumber" is possible if you marked this in the email properties (out-of-the-box setting) and I think you are facing a bug here.
I would not recommend to fix the bug buy yourself with a general business rule setting the target_table field, but let ServiceNow fix this issue out-of-the-box themselves.
 
To answer your second question:
It should be enough defining one Inbound Email Action for the parent table (sn_hr_core_case), it should also be triggered by emails with child tables as target_table.
If your requirements changes in future and you need to perform different actions per child table, you can adjust your inbound action script by this evaluation:
 

 

 

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

 

 

 
 
References:
2*https://developer.servicenow.com/dev.do#!/learn/learning-plans/xanadu/servicenow_administrator/app_s...

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