The CreatorCon Call for Content is officially open! Get started here.

Adding Multiple users from a List Collector Variable to the CC in an Email Script

Peter Williams
Kilo Sage

I am not sure why ServiceNow doesn't have an option to add users to the CC instead of scripting it out but i need help 

 

I have a List Collector on my form where users can add more than one person to the CC of the email that gets sent out

 

what i am having issues is, to get all the user added to the CC. i know how to add one using the email scripts using a Single line text variable but i need to now how to script it for a List collector.

 

email.addAddress("cc", current.variables.ccemail.email, ccemail); 

 

i have look at options like putting it into an Array and using a For loop in the script but it doesnt seem to work

 

Please Help..

 

1 ACCEPTED SOLUTION

Peter Williams
Kilo Sage

i got the script to work now with this alternation

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

    var grUsers = new GlideRecord('sys_user');
    grUsers.addQuery('sys_id', "IN", current.variables.cc_email);
    grUsers.query();
    while(grUsers.next()) {
        email.addAddress("cc", grUsers.email.getDisplayValue()+'', grUsers.name+'');
template.print(grUsers.email.getDisplayValue()+ grUsers.name);
}

})(current, template, email, email_action, event);

View solution in original post

17 REPLIES 17

Peter Williams
Kilo Sage

i tried this and doesnt work

 

email.addAddress("cc", current.variables.ccemail.email, ccemail);
var addAllEmails = [];
var addALLNames = [];
while (next()) {
addAllEmails.push(current.variables.prepared_by.email.toString(), current.variables.ccemail.email.toString())
addALLNames.push(prepared_by.toString(), ccemail.toString())
}
for (var i = 0; i < addAllEmails.length; i++) {
email.addAddress("cc", addALLEmails[i], addALLNames[i]);

}

Peter Williams
Kilo Sage

Any suggestions on how i can proceed with this script

Stefan Georgiev
Tera Guru

Hello @Peter Williams ,

I am not sure how you are passing your data but this should work:

 

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
	
	var users = new GlideRecord('sys_user');
	users.addQuery('sys_id', "IN", current.variables.<all user to include incc>); 
	users.query();

	while (users.next()) {
                email.addAddress("cc",users.email, users.name ); 
	}	
	
})(current, template, email, email_action, event);

 

 

 

this is assuming you have all the users to be included in the notification cc in one variable.Hope that this helps you!

If the provided information answers your question, please consider marking it as Helpful and Accepting the Solution so other community users can find it faster.

All the Best,
Stefan

sorry to say but it didnt work