The Zurich release has arrived! Interested in new features and functionalities? Click here for more

addAddress() in Email scripts

Madhan007
Tera Contributor

Hi Everyone,

I am defining an Email script to add the approver of a HR case in  cc and bcc of a notification triggered by an Event in the Flow. Please find the script below. Current object is the Approval  GlideRecord passed from the event.

var gr = new GlideRecord('sn_hr_core_case');

    if (gr.get(current.sysapproval)) {

        var approvalgr = new GlideRecord('sysapproval_approver');

        approvalgr.addQuery('sysapproval', gr.sys_id);
        approvalgr.query();
        while (approvalgr.next()) {

            if (approvalgr.approver.email) {

                var emailString = approvalgr.approver.email.toString();
                var displayName = approvalgr.approver.name;
                email.addAddress("cc", emailString, displayName);

            }
        }

 

    }

 

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

 

I logged all the details using gs.info and everything is fine,  except that cc and bcc are not being populated in Email logs.

when I run the same script through Background scripts, its throwing an error sayin that email is not defined

Please pour your Insights.

Thank you!

1 ACCEPTED SOLUTION

Chaitanya ILCR
Mega Patron

Hi @Madhan007 ,

 

you script looks good it should work

cc and bcc will be populated in the copied and Blind Copied field of the email

ChaitanyaILCR_0-1751024894445.png

 

you can also use this script but your script should work fine

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

    var approvalgr = new GlideRecord('sysapproval_approver');

    approvalgr.addQuery('sysapproval', current.getValue('sysapproval'));
    approvalgr.query();
    while (approvalgr.next()) {

        if (approvalgr.approver && approvalgr.approver.email) {

            var emailString = approvalgr.approver.email.toString();
            var displayName = approvalgr.approver.name;
            email.addAddress("cc", emailString, displayName);

        }
    }


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

in the background script context the email parameter is undefined so it will throw the error

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

4 REPLIES 4

Nilesh Pol
Tera Guru

@Madhan007 verify below link for the reference: cc & bcc on email - ServiceNow Community

Ankur Bawiskar
Tera Patron
Tera Patron

@Madhan007 

you can't test it from background script.

Points to check

1) are those users active and not locked out?

2) are those users having email address populated in user record?

3) are those users having notification preference enabled?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Madhan007 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Chaitanya ILCR
Mega Patron

Hi @Madhan007 ,

 

you script looks good it should work

cc and bcc will be populated in the copied and Blind Copied field of the email

ChaitanyaILCR_0-1751024894445.png

 

you can also use this script but your script should work fine

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

    var approvalgr = new GlideRecord('sysapproval_approver');

    approvalgr.addQuery('sysapproval', current.getValue('sysapproval'));
    approvalgr.query();
    while (approvalgr.next()) {

        if (approvalgr.approver && approvalgr.approver.email) {

            var emailString = approvalgr.approver.email.toString();
            var displayName = approvalgr.approver.name;
            email.addAddress("cc", emailString, displayName);

        }
    }


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

in the background script context the email parameter is undefined so it will throw the error

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya