add users to CC field from both User table and Emails (Non Users)

Imran1
Giga Guru

Hi All,

I am trying to include users who are part of a field (Users & Non-Users) into the cc (copied) field using the email script but my script only adds the users from sys_user table and non-users are excluded. Please let me know a way to combine users plus non-users

With the below script if I have three email ids (one part of user table) and two non-users only user is added to copied field in the notification. How to include the non-users as well

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

if (!current.trigger_id.u_string_7.nil()) {

var tbl = current.trigger_table;
var sysID = current.trigger_id;
var ccIds = current.trigger_id.u_string_7.split(",");

var user = new GlideRecord("sys_user");
user.addQuery("email", ccIds);
user.query();
while (user.next()) {
email.addAddress("cc", user.email, user.getDisplayValue());
}
}

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

Appreciate your response. 

Thank You,
Imran

1 ACCEPTED SOLUTION

Hey Imran,
Its working for me, where are you checking for copied list, update your list/form layout to bring "Copied" field.

   var ccIds = ["suzette.devaughan@example.com", "aman123@test.com", "imran456@test.com"];
    template.print(ccIds);
	
    for (var emailIndex in ccIds) {
        var user = new GlideRecord("sys_user");
        user.addQuery("email", ccIds[emailIndex]);
        user.query();
        if (user.next()) {
            email.addAddress("cc", user.email, user.getDisplayValue());
        } else {
            email.addAddress("cc", ccIds[emailIndex], ccIds[emailIndex]);
        }
    }

 

find_real_file.png

Best Regards
Aman Kumar

View solution in original post

7 REPLIES 7

Hey Imran,
Its working for me, where are you checking for copied list, update your list/form layout to bring "Copied" field.

   var ccIds = ["suzette.devaughan@example.com", "aman123@test.com", "imran456@test.com"];
    template.print(ccIds);
	
    for (var emailIndex in ccIds) {
        var user = new GlideRecord("sys_user");
        user.addQuery("email", ccIds[emailIndex]);
        user.query();
        if (user.next()) {
            email.addAddress("cc", user.email, user.getDisplayValue());
        } else {
            email.addAddress("cc", ccIds[emailIndex], ccIds[emailIndex]);
        }
    }

 

find_real_file.png

Best Regards
Aman Kumar

Hi Aman,

It is working now, within the first script this was missing when added it worked

user.addQuery("email", ccIds[emailIndex]);

Could you please explain how this works - the below portion of the script

for (var emailIndex in ccIds) {

Thanks Again
Imran

This works like foreach, emailIndex starts iterating from beginning to end of array "ccIds", and you can use this as index to fetch values from array at those indexes ie ccIds[emailIndex]

Best Regards
Aman Kumar