- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2023 07:37 AM
Hello,
We have a custom field users and also we have new email address. If we enter any external email address and if we select any users in watch list need to send notification and add these users as ''CC'' in email notification.
I have written below mail script and not working. I have added screenshots please check and give suggestions
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2023 08:41 AM - edited 05-22-2023 08:46 AM
Hey @Rakesh50, here's a full blown script block you can insert inside of your mails script to add both users, as well as external email address to the CC field. I have broken it up to sections and added comments so you can follow what it does,, and change it to your needs as required.
I have tested and it's working as expected (make sure the list field name, e.g. u_users is correct!).
try {
/* Create an array from the user list values */
var getListusers = current.u_users.split(',');
/* Create a separate array with email addresses using RegEx */
var r = new RegExp('([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)', 'jgi');
var emailArr = getListusers.filter(function(x) {
return x.match(r) ? true : false;
});
/* Reduce the original array to users only */
var arrayUtil = new ArrayUtil();
var userArr = arrayUtil.diff(getListusers, emailArr);
/* Add users to CC */
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('sys_idIN' + userArr + '^notification=2^emailISNOTEMPTY');
userGr.query();
while (userGr.next()) {
email.addAddress('cc', userGr.email, userGr.getDisplayValue());
}
/* Add email addresses to CC */
var emailAddress = '';
for(i = 0; i < emailArr.length; i++) {
emailAddress = emailArr[i].toString();
email.addAddress('cc', emailAddress);
}
} catch(err) {
gs.log('users_to_cc mail script failed: ' + err);
}
Here are some screenshots too.
- Calling the mail script from the notification records body (inn may case, the mail script is called 'users_to_cc', make sure you use the correct name):
- Populated CC field in the email record:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2023 03:44 AM
Thank you!!
If you found my response helpful, please mark it as correct and close the thread to benefit others.
Regards,
Kaustubh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2023 08:41 AM - edited 05-22-2023 08:46 AM
Hey @Rakesh50, here's a full blown script block you can insert inside of your mails script to add both users, as well as external email address to the CC field. I have broken it up to sections and added comments so you can follow what it does,, and change it to your needs as required.
I have tested and it's working as expected (make sure the list field name, e.g. u_users is correct!).
try {
/* Create an array from the user list values */
var getListusers = current.u_users.split(',');
/* Create a separate array with email addresses using RegEx */
var r = new RegExp('([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)', 'jgi');
var emailArr = getListusers.filter(function(x) {
return x.match(r) ? true : false;
});
/* Reduce the original array to users only */
var arrayUtil = new ArrayUtil();
var userArr = arrayUtil.diff(getListusers, emailArr);
/* Add users to CC */
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('sys_idIN' + userArr + '^notification=2^emailISNOTEMPTY');
userGr.query();
while (userGr.next()) {
email.addAddress('cc', userGr.email, userGr.getDisplayValue());
}
/* Add email addresses to CC */
var emailAddress = '';
for(i = 0; i < emailArr.length; i++) {
emailAddress = emailArr[i].toString();
email.addAddress('cc', emailAddress);
}
} catch(err) {
gs.log('users_to_cc mail script failed: ' + err);
}
Here are some screenshots too.
- Calling the mail script from the notification records body (inn may case, the mail script is called 'users_to_cc', make sure you use the correct name):
- Populated CC field in the email record: