How to get recipient list inside email notification script ?

ASA5
Kilo Sage

Hello ,

I want to get the list of recipient inside a notification email script.

Here is the code i used inside the notification email script.

var recipients   = email.recipients;

gs.log("recipients :   "+recipients,"me");

When i check the system log i found recipient empty.

9 REPLIES 9

Yes,


You should create another event for this and proceed.



Thanks,


Mihir


Why does he need an event?


He is saying that email.recipients in a mail_script returns nothing. I don't see how your suggestions would help.


I've tried it myself and indeed email.recipients doesn't return anything in a mail script. There must be a method that should give that value. For example I've seen that email.getSubject() works, but I don't know what I should call for the recipients. Tried with email.getRecipients() but didn't work.


Any clues?


Joe72
Tera Contributor

As of London, we still do not have access to the email.getRecipients() function. Here is the equivalent code:

var arrUtil = new ArrayUtil();
var emails = [];
//Get sys_ids of reference fields
var ids = [];
if(email_action.recipient_fields) {
	var r = email_action.recipient_fields.split(',');
	for(var x = 0; x < r.length; x++)
		ids.push(current[r[x]]);
}
//Get user list
ids.push(email_action.recipient_users);
//Get group list
ids.push(email_action.recipient_groups);
//If parm1 contains recipient, get parm1 value
if(email_action.event_parm_1)
	ids.push(event.parm1);
//If parm2 contains recipient, get parm2 value
if(email_action.event_parm_2)
	ids.push(event.parm2);
//Re-split the array by commas
ids = ids.toString().split(',');

//Check if any of these are group ids
//Find the users and add them to the ids array
var groupMem = new GlideRecord('sys_user_grmember');
groupMem.addQuery('group.sys_id', ids);
groupMem.query();
while(groupMem.next()) {
	//If the group has an email set, add the email and skip adding the users individually
	if(groupMem.group.email)
		emails.push(groupMem.group.email);
	else
		ids.push(groupMem.getValue('user'));
}

var finalIds = [];
//Get the users' emails
var users = new GlideRecord('sys_user');
users.addQuery('sys_id', ids);
users.query();
while(users.next()) {
	finalIds.push(users.getValue('sys_id'));
	emails.push(users.getValue('email'));
}
emails = arrUtil.unique(emails);

//Final list of recipient ids
gs.log("Final Recipient IDs: " + finalIds + " " + finalIds.length);
//Final list of recipient emails
gs.log("Final Recipient emails: " + emails + " " + emails.length)

That being said, due to not being able to modify the recipient list, you'll have to do any recipient modifications in a before-insert business rule on the email table.

 

Ben Beetle
Tera Contributor

 

It appears this only works if r[x] is not a dot walked field

current[r[x]]

 

 

To get around this you can do

current.getDisplayValue(r[x] + '.sys_id') 

 

Credits to Pok for helping me figure this out on sndevs slack! 

 

BartekM
Tera Contributor

Hi, just had the same issue.

 

Got email address for the user using

var rep = event.parm1

email was being generated by the event and we have access to event object from within the mail script

 

Hope this helps to a user searching for the answer these days, as original post is from 2016