- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 10:10 AM
How to add multiple emails in CC in Email script? will below code work? please fix it if it is wrong script..
var getNumbers = event.parm2.toString();
var addALLEmails=[];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('numberIN' + getNumbers);
gr.query();
while (gr.next()) {
var email1 = gr.user1.email;
var email2 = gr.user2.email;
var email3 = gr.user3.email;
addALLEmails.push(email1.toString() + email2.toString() +email2.toString())
}
email.addAddress("cc",addALLEmails,"Incidents");
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 10:19 AM
Hello,
Few ways to do it, but here's one that stays inline when what you're already doing...
var getNumbers = event.parm2.toString();
var addALLEmails=[];
var addALLNames=[];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('numberIN' + getNumbers);
gr.query();
while (gr.next()) {
addALLEmails.push(gr.user1.email.toString(),gr.user2.email.toString(),gr.user3.email.toString());
addALLNames.push(gr.user1.name.toString(),gr.user2.name.toString(),gr.user3.name.toString());
}
for (var i = 0; i < addALLEmails.length; i++) {
email.addAddress("cc",addALLEmails[i],addALLNames[i]);
}
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 10:19 AM
Hello,
Few ways to do it, but here's one that stays inline when what you're already doing...
var getNumbers = event.parm2.toString();
var addALLEmails=[];
var addALLNames=[];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('numberIN' + getNumbers);
gr.query();
while (gr.next()) {
addALLEmails.push(gr.user1.email.toString(),gr.user2.email.toString(),gr.user3.email.toString());
addALLNames.push(gr.user1.name.toString(),gr.user2.name.toString(),gr.user3.name.toString());
}
for (var i = 0; i < addALLEmails.length; i++) {
email.addAddress("cc",addALLEmails[i],addALLNames[i]);
}
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 10:24 AM
Thanks,
is there any possibility to exclude names one? because in ADD Emails array we may had a chance to remove duplicate ones.. so to avoid that conflict I want to avoid it..

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 05:55 AM
Hi,
The solution I've provided above was done the way it was because it seemed like you were grabbing numerous fields and had already set it up this way.
When CC's are added, even if it's a duplicate, it won't go out twice the to same person. It will only be sent once.
So you could use ArrayUtil API and then use the unique method to remove duplicates, technically...you shouldn't have to as the emails won't go out multiple times to the same person even if they're included 2,3,4 times as a CC.
Please try for yourself and see.
Only you know why you have a user1, user2, user3 fields in your script. So we can't really tell you it's unnecessary as you may have a need for doing that?
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 08:09 PM
Hi,
since you are already iterating using while loop just keep email.addAddress() within it and no need of extra 2 variables for email and name as it would consume unnecessary memory space
var getNumbers = event.parm2.toString();
var gr = new GlideRecord('incident');
gr.addEncodedQuery('numberIN' + getNumbers);
gr.query();
while (gr.next()) {
email.addAddress("cc", gr.user1.email.toString(), gr.user1.getDisplayValue());
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader