The CreatorCon Call for Content is officially open! Get started here.

How do you set a recipient of a notification based on conditions?

neil_b
Tera Guru

I was trying to accomplish the following: 

If a user gets deactivated, then trigger an email to send to the person who the request is for, but if the requested user is inactive, then send the email to Admins. I was able to sort of get this to work with this mail script using the CC field but with ServiceNow's OOB functionality, if the recipient is inactive, the email does not send at all. 

            var requestUser = current.requested_for; 
			var grUser = new GlideRecord('sys_user');
			grUser.addEncodedQuery('sys_id=' + requestUser + '^active=true');
			grUser.query();
			if(grUser.next()){
		email.addAddress('cc', current.requestUser.email.toString(), current.requestUser.name.toString());
					} else {
		email.addAddress('cc', 'adminEmail()', 'adminName'());
					}

 How do I accomplish this because I know that mail scripts can only handle "CC" and "BCC" but not "Recipients". I'm not sure how to set the recipient based on certain conditions without using a mail script. 

1 REPLY 1

AnirudhKumar
Mega Sage

You will need to fire your notification using an event. 

So, the flow will be like this...

Business rule runs-> execute gs.eventQueue() -> trigger notification.

Basically, the based on your condition, eventQueue function will carry different parameters. These parameters will be sysids of users who you want to send your notification to. 

 

Just google 'eventQueue ServiceNow' to know more.