getting cc in email
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2023 10:13 PM
Hi I have a requirement , in an email I have to get CC recipients , in the table where we generating the mail we have a reference type of field, this reference field is referencing to a table where are 2 fields which is referencing to sys user table , so in that mail we have to fetch these 2 users in email cc , but it is not fetching , I have written this code below -
(function runMailScript(current, template, email, email_action, event) {
var delegateArray = [];
var portfolioLead = current.u_product_id.u_product_attribute.u_product_manager;
delegateArray.push(portfolioLead);
var prodMng = current.u_product_id.u_product_attribute.u_portfolio_lead;
delegateArray.push(prodMng);
delegateArray.push(portfolioLead);
delegateArray.push(prodMng);
for (var j = 0; j < delegateArray.length; j++){
var gr = new GlideRecord('sys_user');
if(gr.get(delegateArray[j])){
email.addAddress("cc", gr.email);
}
}
})(current, template, email, email_action, event);
here u_product_id is the reference field ,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2023 10:25 PM - edited ‎10-08-2023 11:48 PM
Hi @Amit Dey1 ,
Your code looks mostly correct, but it seems you're using `delegateArray` to store both `portfolioLead` and `prodMng` twice, and you're looping through `delegateArray` twice, which might be redundant. Also, ensure that `gr.email` has a valid email address in your ServiceNow instance.
Here's a slightly optimized version of your script:
(function runMailScript(current, template, email, email_action, event) {
var portfolioLead = current.u_product_id.u_product_attribute.u_product_manager;
var prodMng = current.u_product_id.u_product_attribute.u_portfolio_lead;
var usersToCC = [portfolioLead, prodMng];
for (var i = 0; i < usersToCC.length; i++) {
var gr = new GlideRecord('sys_user');
if (gr.get(usersToCC[i])) {
email.addAddress("cc", gr.email);
}
}
})(current, template, email, email_action, event);
In this version:
1. `usersToCC` array stores the users you want to CC.
2. The loop iterates through this array, fetching each user's email and adding them to the CC recipients.
Please ensure that:
- `portfolioLead` and `prodMng` have valid user IDs.
- The `email` object is correctly set up to handle CC addresses.
- `gr.email` contains a valid email address for each user record.
Mark my answer helpful & accepted if it helps you resolve your issue.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2023 10:53 PM - edited ‎10-08-2023 11:05 PM
Hi @Amit Dey1 ,
You can use below script in that product lead and product manager push to delegateArray and also checking if we have duplicate users in array and get only unique users to add in CC and also make sure current object getting correct data.
(function runMailScript(current, template, email, email_action, event) {
var delegateArray = [];
var portfolioLead = current.u_product_id.u_product_attribute.u_product_manager;
var prodMng = current.u_product_id.u_product_attribute.u_portfolio_lead;
if (portfolioLead) {
if (delegateArray.indexOf(portfolioLead) === -1) {
delegateArray.push(portfolioLead);
}
}
if (prodMng) {
if (delegateArray.indexOf(prodMng) === -1) {
delegateArray.push(prodMng);
}
}
for (var j = 0; j < delegateArray.length; j++) {
var gr = new GlideRecord('sys_user');
if (gr.get(delegateArray[j])) {
email.addAddress("cc", gr.email);
}
}
})(current, template, email, email_action, event);
Please mark it solution proposed and helpful if it works for you.
Thanks,
Anand