We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

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

peterkopeck
Tera Contributor

I'm encountering the same issue, any update on this one ?

Rachel55
Mega Guru

I never got it working.

Aanchal Agarwa1
Tera Contributor

Hey @Rachel55 ,

 

Two things to check:

  1. Use getValue('field_name') to pass field values instead of accessing them directly
  2. Use 'cc' in lowercase as recommended by ServiceNow

Find the corrected code snippet below for reference.

 

(function runMailScript(current, template, email, email_action, event) {

    var userGR = new GlideRecord('sys_user');
    userGR.addEncodedQuery("email!=NULL");
    userGR.setLimit(5);
    userGR.query();

    while (userGR.next()) {
        var emailAddr = userGR.getValue('email');
        if (emailAddr) {
            gs.log('cc email: ' + emailAddr, 'EmailScript');
            email.addAddress("cc", emailAddr, userGR.getValue('name'));
        }
    }

    template.print("Aanchal was here");
   
})(current, template, email, email_action, event);

 

Also — to verify if CC is actually being added, don't check the Recipients field on sys_email table. CC and BCC recipients are not shown there. Instead check the Copied field on the same sys_email record — that's where CC addresses will appear.

AanchalAgarwa1_0-1782409221667.png

Ref

https://www.servicenow.com/community/developer-forum/mail-script-email-addaddress-not-working/m-p/18...

https://www.servicenow.com/docs/r/platform-administration?topicPivot=platadm-r_ExScptEmlNtfn

 

Thanks
Aanchal
Please Accept the solution if it assisted you with your question & Mark this response as Helpful.