Add a Recipient to Notification if a field on Change form is not null - Email Script

mattmm
Kilo Sage

Hi all, I have successfully built a mail script to add to a subject line if a field is not empty, however I now also need this to add a recipient to the outbound notification email, if that same field is populated.

 

i.e. if the  current.u_vendor_reference_number != '' then add matt.test@matt.com.au to the list of existing recipients of that notification. 

 1. the u_vendor_reference_number is on the change_request table

2. I not only need this on change related notifications, but also on the approval notifications (using OOB currently)

3. This is my current mail script I'd like to add to if possible -

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


    var subj = email.getSubject();//get current subject from notification
    var checkref = current.u_vendor_reference_number;
    if (checkref != '') {
        email.setSubject(subj + ' | Ticket #' + checkref);

    }

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

 

 

1 ACCEPTED SOLUTION

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @mattmm - You can do something like this:

 

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

    var subject = email.getSubject();
    var vendorReferenceNumber = current.u_vendor_reference_number;

    if (vendorReferenceNumber) {
        email.setSubject(subject + ' | Ticket # ' + vendorReferenceNumber.getDisplayValue());
        email.addAddress('cc', 'matt.test@matt.com.au', 'Matt Test');
    }

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

 

 Just keep in mind that the addAddress can only be used to append to the 'cc' or 'bcc' list.

View solution in original post

6 REPLIES 6

Even without email sending enabled, you should see the cc'd address in the "Copied" field on sys_email. Is that what you're looking at? You won't see the complete headers if an email isn't sent, so you won't have the "Cc:" line there.

Perfect. I wasn't looking at this field, I was looking at recipients and headers! Thankyou so much