- 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-19-2023 08:52 AM
Hii @Rakesh50
For that you can store all the email addresses in one array and then you can use something like following to add all users in cc. If its user add get there email address by using glideRecord and store in array.
Kindly mark my answer as Correct and helpful based on the Impact.
Regards,
Siddhesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2023 09:09 AM - edited 05-19-2023 09:10 AM
@Siddhesh Gawade Yes you are correct. I have tried same code which is similar to your code. But not works. Because users list type field returning sys_id. I have noticed that in logs. Tried below code
if(!current.u_users.nil()){
var watcherIds = current.u_users.split(",");
gs.log("watcherIds-->"+watcherIds);
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", watcherIds);
user.addQuery("notification",2);
user.addQuery("email","!=","");
user.query();
while(user.next()){
email.addAddress("cc", user.email, user.getDisplayValue());}}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2023 09:18 AM
If this the issue you can just separate the sys_ids from email addresses and using glideRecord you can get email addresses for those sys_ids as well. once you got all email addresses in one array you are good to go with the provided script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2023 09:22 AM
@Siddhesh Gawade But i'm struck in separation. I want to know how to separate email id's from array and also i need these separated email address too for sending them in "CC" in notification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2023 11:18 AM
Hello @Rakesh50
You can for loop to check if the array idex contains some string which is similar in all emails
arr =[]//which contains all values including sys IDs and email addresses
Email=[];
SysID =[];
for (var i = 0; i < arr.length; i++) {
if(arr[i].contains("gmail.com"){//you can use any string value
Email.push(arr.[i];
}
else{
SysID.push(arr[I];
}
}
After this you can separated sysids and email addresses. Then you can get emails for sysid array using GlideRecord and for loop. And then just join those two email address arrays. And then finally use script to set cc addresses.