Recipient email address and the mail script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 04:33 AM
Hello all,
I'm attempting to generate dynamic notification content based on the recipient's email domain.
I wrote a mail script to meet the need, however it isn't working. Is there anything I've missed?
Thank you
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var checkRecipient = email.recipients;
if (checkRecipient.indexOf('@gmail.com') > -1)
{
template.print('<div class=font><p><font size="2">content for gmail accounts</font></p></div>');
} else {
template.print('<div class=font><p><font size="2">content for internal users</font></p></div>');
}
})(current, template, email, email_action, event);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 04:46 AM
Hi @tsoct ,
I think its the issue with 'email.recipients' but i have tried something similar on my PDI and its working fine.
Just change your mail script to following and check if that works:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
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(',');
var checkRecipient = ids.join();
if (checkRecipient.indexOf('@gmail.com') > -1) {
template.print('<div class=font><p><font size="2">content for gmail accounts</font></p></div>');
} else {
template.print('<div class=font><p><font size="2">content for internal users</font></p></div>');
}
})(current, template, email, email_action, event);
Regards,Sushant Malsure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 08:57 AM
Hello, thanks for the script. ids seems to return sys_id of the users. How should i convert them to email? Since some of the users in our system registered with gmail too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 11:47 AM
Hi @tsoct : Please ignore my previous comment.
Here is updated script to convert IDs to email and then check the gmail account.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
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(',');
for(var y =0; y<ids.length; y++){
var getEmail = new GlideRecord('sys_user');
getEmail.get(ids[y]);
emails.push(getEmail.email);
}
var checkRecipient = emails.join();
if (checkRecipient.indexOf('@gmail.com') > -1) {
template.print('<div class=font><p><font size="2">content for gmail accounts</font></p></div>');
} else {
template.print('<div class=font><p><font size="2">content for internal users</font></p></div>');
}
})(current, template, email, email_action, event);
Try and let me know how it goes.
Regards,Sushant Malsure