Sudhanshu Talw1
Tera Guru

Hey folks hope everyone is doing good.

We recently came to a scenario explained below where we need to handle Secondary email of the User.

Scenario:  We have users which have two Email ID's say one with @domain1.com & other with @domain2.com. Let's say the primary is @domain1.com. Now there is a rule which is written on the @domain1.com mail exchange which will redirect any incoming mail to the @domain2.com.

 

Issues: Suppose the user got the approval email on @domain2.com(redirected from @domain1.com)  which has the approval button to approve the RITM or link to a ticket or HR case on which the user can reply & expect that RITM will be approved & comment will be added to the ticket or HR case itself. 

In servicenow you can create multiple email devices, so we just created the one named as secondary email address & added the required notifications in it.

Created a request whose RITM needs to be approved by me. Setting up secondary email device worked & got the email on the Secondary mail (@domain2.com). The email itself has Approve/Reject button. Clcked on approve & sent it to Servicenow instance. 

Expected behaviour: The RITM should get approved.

Actual behaviour: No action on RITM with that email.

Quick Findings: The only difference we found in the recieving emails is that the User ID field & User field is not set in case of Secondary email while in case of Primary email Servicenow sets it automatically.

So here comes the solution:

We created a secondary email field on the [sys_user] table to hold the secondary email address & wrote a before insert BR to set the user in the incoming email.

Create a before insert BR:

Table: Email[sys_email]

Condition: Headers | contains | @domain2.com AND

Type | is | recieved

Advanced => Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var emailheader = current.getValue('headers');
    var emails = extractEmails(emailheader);
    //Fetch the Secondary email ID of the user at first position
    var userSecEmail = emails[0];
    var userGR = new GlideRecord("sys_user");
    userGR.addEncodedQuery("active=true^u_secondary_email=" + userSecEmail);
    userGR.query();
    if (userGR.next()) {
        var user = userGR.getUniqueValue();
        current.user_id = user;
        current.user = user;
    }
})(current, previous);

//Extract Email ID's from the header & return array of Email Id's:
function extractEmails(text) {
    return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
}


Sample Header format recieved in the incoming email:
The return path is the EmailID where the email was sent from to Servicenow.

Return-Path:<acooke@domain2.com>
Delivered-To:dev1111.service-now.com

Guess what...pleased as punch.

It worked in case of both addition of comments on ticket & Approval/Rejection as well.

Some of the questions that could be raised here:

Why we created dictionary entry rather than Secondary email notification device?

The answer here is simple:

We just want to store the email address for a short duration of time. So we choose the same. Next thing setting up secondary email device will end up in sending multiple emails to @domain2.com.

Note: Also it should be noted no matter what we are setting up either Secondary Notification device or dictionary entry, the above code can be tweeked out to lookup Secondary Notification device as well.

 

 

Thanks

Sudhanshu

 

 

Version history
Last update:
‎12-22-2021 03:07 AM
Updated by: