Check for valid user by a secondary email address for inbound actions?

Marcel H_
Tera Guru

I have an issue that I'm sure isn't unique, but after reading quite a few posts here I'm not sure if there is a good, system wide way to check a custom field on sys_user for an email address related to a new inbound message.

We have a field on sys_user "u_alternate_email" that is used for some users that we have who technically belong to two different companies and have two different email addresses. Originally there were two separate user accounts, but that created a lot of confusion for these users, and related records were split between the two, so it was hard to see everything related to that person in one place. There were also issues with SSO authentication with two different accounts.

Now, in a few cases, we have users that are have sent inbound mail to the instance with their alternate email address, but the system ignores it because it's not in the OOB email field. Is there a way that we can globally define a business rule for inbound email that would associate to the user's record if either email or u_alternate_email is the same as the sender's address?

I've seen examples where something similar has been done in individual inbound action scripts, but I'd like to avoid updating potentially 100s of specific inbound action scripts to account for all possible cases where this might occur.

Any thoughts or suggestions are appreciated!

1 ACCEPTED SOLUTION

Giles Lewis
Giga Guru

This problem was solved several years ago in this blog post by Brian Dailey:

The solution is a before insert/update business rule on the sys_email table. Brian's original solution was based on using cmn_notif_device to obtain the alternate email address. We modified it to use an alternate email address in sys_user, just as you are doing.

Here is the code for our business rule.

 

(function executeRule(current, previous /*null when async*/) {
	// Update received email based on match of alternate email.
	
	// Based on the following blog post by Brian Dailey:
	// https://community.servicenow.com/community?id=community_blog&sys_id=5a5da629dbd0dbc01dcaf3231f961937
	
	var myname = '<<name of this business rule>>';

	// If current.user_id is set then the OOB code must have matched on email
	if (current.user_id) return;
		
	var sender = current.getValue('user');
	if (!sender) {
		gs.log(myname + ': No sender address', myname);
		return;
	}
	// Reply To field is used OOB only for Sent emails
	// So use this field to save the sender's email address
	current.reply_to = sender;
	
	matchUserByAltEmail();
	
	if (!current.user_id) {
		gs.log(myname + ': No user found for address: ' + current.user, myname);		
	}
	
	function matchUserByAltEmail(){
		// Match on u_alternate_email
		var grUser = new GlideRecord('sys_user');
		if (sender && grUser.get('u_alternate_email', sender)) {
			current.user_id = grUser.getUniqueValue();
			current.user = grUser.getUniqueValue();
			gs.log('Email ' + current.sys_id + 
				' matched ' + sender + 
				' to u_alternate_email for ' + grUser.name, myname);			
		}	
	}

		
})(current, previous);

View solution in original post

5 REPLIES 5

Prateek kumar
Mega Sage

How about you write an insert before Business rule and check if the email is received from alternate email address and set the user id accordingly.

 

Please mark my response as correct answer and helpful if it helped solved your question.
-Best Regards
Prateek kumar


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Thanks, I was thinking a BR on sys_email to perform the check would probably be the way to go. I’ll need to see how to script the check of the inbound address against sys_user and set get the right user account to the inbound mail.

Giles Lewis
Giga Guru

This problem was solved several years ago in this blog post by Brian Dailey:

The solution is a before insert/update business rule on the sys_email table. Brian's original solution was based on using cmn_notif_device to obtain the alternate email address. We modified it to use an alternate email address in sys_user, just as you are doing.

Here is the code for our business rule.

 

(function executeRule(current, previous /*null when async*/) {
	// Update received email based on match of alternate email.
	
	// Based on the following blog post by Brian Dailey:
	// https://community.servicenow.com/community?id=community_blog&sys_id=5a5da629dbd0dbc01dcaf3231f961937
	
	var myname = '<<name of this business rule>>';

	// If current.user_id is set then the OOB code must have matched on email
	if (current.user_id) return;
		
	var sender = current.getValue('user');
	if (!sender) {
		gs.log(myname + ': No sender address', myname);
		return;
	}
	// Reply To field is used OOB only for Sent emails
	// So use this field to save the sender's email address
	current.reply_to = sender;
	
	matchUserByAltEmail();
	
	if (!current.user_id) {
		gs.log(myname + ': No user found for address: ' + current.user, myname);		
	}
	
	function matchUserByAltEmail(){
		// Match on u_alternate_email
		var grUser = new GlideRecord('sys_user');
		if (sender && grUser.get('u_alternate_email', sender)) {
			current.user_id = grUser.getUniqueValue();
			current.user = grUser.getUniqueValue();
			gs.log('Email ' + current.sys_id + 
				' matched ' + sender + 
				' to u_alternate_email for ' + grUser.name, myname);			
		}	
	}

		
})(current, previous);

Perfect! Thanks for sharing your code, it was almost like it was written specifically for our instance haha. This did the trick exactly as needed and I’ve bookmarked the original post you linked for reference as well just in case we need something similar in the future too.