How to add multiple emails in CC in Email script

Sathwik1
Tera Expert

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"); 

@Ankur Bawiskar  @Aman Kumar @Jaspal Singh 

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

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!

View solution in original post

5 REPLIES 5

Allen Andreas
Administrator
Administrator

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!

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..

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!

Ankur Bawiskar
Tera Patron
Tera Patron

@Sathwik 

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader