Chris Pearson
Tera Contributor

Disclaimer: The ideas, thoughts, and opinions in this post reflect my own views and do not necessarily represent the views of my employer, Accenture. 

The published API of ServiceNow is quite large and continues to grow. I'm sure these methods I'm about to share have been around for a while, but I just recently bumped into them and thought I'd share. They're pretty cool. And I've certainly had past requirements come across my desk where I wish I had known about them. 

Here's the scenario. You have an email notification which is sent from ServiceNow but determining who that email should be sent to or other attributes of the email itself may be dynamic...too dynamic to simply set to the 'To' on the email to the value of one of the fields on the source record it's triggered from. 

Enter GlideEmailOutbound! 

There are some really handy methods out there which allow you to easily change attributes of the outbound email as it's being sent. The easiest way to do this is from within a Notification Email Script. If using these methods from within a Notification Email Script, the 'email' object is already defined as an instantiation of the GlideEmailOutbound class. This means you can simply call these methods using 'email.<function name>' as noted below in these examples. 

p.s. Don't forget to actually call the Notification Email Script from within your Notification.

addAddress() method

Example code used in Notification Email Script:

var user = new GlideRecord('sys_user');
user.addEncodedQuery('your_query_goes_here');
user.query();
while(user.next()){
	var emailAddress = user.email.toString();
	if(!gs.nil(emailAddress)){
		email.addAddress('bcc', emailAddress, user.getDisplayValue());
	}
}

setFrom() method

Example code used in Notification Email Script:

var emailAddress = getEmail();
email.setFrom(emailAddress);

function getEmail(){
	// Some type of code which determines what email
	// address is to be used as the 'from' on the
	// outbound email notification
}

setReplyTo() method

Example code used in Notification Email Script:

var emailAddress = getEmail();
email.setReplyto(emailAddress);

function getEmail(){
	// Some type of code which determines what email
	// address is to be used as the 'reply to' on the
	// outbound email notification
}