email.addAddress() not working

Rachel55
Mega Guru

Can anyone tell me why this script does not update the 'copied' field in the email log.  The log statement does display the correct email addresses.

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
    if (current.cat_item == "c2aed0a31b6026901c7d62c7b04bcbcd") {
        var loc = current.request_item.variables.site_br;
        gs.log("cnc loc " + loc);
    }
    try {
        var inputs = {};
        inputs['u_location'] = loc.sys_id; // Reference, Sys ID of the record

        var dt = new sn_dt.DecisionTableAPI();
        var response = dt.getDecision('e31affcb1bdc9a908d96653b234bcb06', inputs);

        var result_elements = response.result_elements;
        var u_network_tech = result_elements.u_network_tech.getValue(); // Reference
        var u_pc_tech = result_elements.u_pc_tech.getValue(); // Reference
        var u_supervisor = result_elements.u_supervisor.getValue(); // Reference


    } catch (e) {
        gs.log("Couldn't run this script Error: " + e)
    }
    var com_list = [];
    com_list.push(u_network_tech);
    com_list.push(u_pc_tech);
    com_list.push(u_supervisor);
   
    for (var i = 0; i < com_list.length; i++) {
        if (com_list[i]) {
            var userGR = new GlideRecord('sys_user');
            userGR.addQuery("sys_id", com_list[i]);
            userGR.query();
            while (userGR.next()) {
                gs.log("cnc email " + userGR.email);
                var name = userGR.first_name + " " + userGR.last_name;
                email.addAddress("CC", userGR.email, name);

            }
        }
    }
         

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

Rafael Batistot
Tera Sage

Hi @Rachel55 

 

So, your script is correct the reason you don’t see updates in the Email Log is that the copied field isn’t written during the mail script execution.

 

That’s why you won’t see the copied field update at mail script runtime it’s only populated when the outbound message is finalized into the sys_email record.

Swapna Abburi
Mega Sage
Mega Sage

Hi @Rachel55 

I hope it doesn't matter but can you try with 'cc' string type in lower case?

kaushal_snow
Mega Sage

Hi @Rachel55 ,

 

You need to use email.addAddress() Properly as in your email notification M\mail Script, include CC or BCC like:

 

(function runMailScript(current, template, email, email_action, event) {
email.addAddress("cc", "user@example.com", "User Name");
email.addAddress("bcc", "secret@example.com", "Secret example");
})(current, template, email, email_action, event);


Ensure these scripts are included via ${mail_script:your_script_name} in the Notification record. After that, confirm Email is Sent, Not Just Previewed, because CC/BCC are handled during message finalization, always verify by checking the record under System Mailboxes → Outbound → Sent.

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Rachel55
Mega Guru

This did not work.  I am using email.addAddress("cc", userGR.email, name).  The rest of the mail script is working fine, it just does not add the recipients.