How to get recipient list inside email notification script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2016 07:00 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2016 09:26 AM
Yes,
You should create another event for this and proceed.
Thanks,
Mihir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2016 06:12 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2019 02:06 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2020 09:16 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 03:01 AM
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