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

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.

Hi @Sheldon  Swift , this doesn't work using the below with my script as suggested. No CC appears in the email logs, the first part of my script still works. Any other ideas?

email.addAddress('cc', 'matt.test@matt.com.au', 'Matt Test');

. Email logs show no CC is added.......

I'm using this notification email script within the email template of the notification (the OOB change.itil.role template part of the OOB Change notifications). I've tried my mail script with your addition directly on the notification also to no avail

Look at System Properties > Email Properties. Within your Outbound Mail Configuration, make sure "Send all email to this test email address (non-production testing)" is empty.

 

sheldondotswift_0-1723004928368.png

Date: Tue, 6 Aug 2024 21:28:01 -0700 (PDT)
From: IT Service Desk <xzyz@service-now.com>
Reply-To: IT Service Desk <xyz@service-now.com>
To: admin@example.com
Cc: Matt Test <matt.test@matt.com.au>

 

hi @Sheldon  Swift , thanks for the reply! I do have an entry in there, but being a dev environment, and a test, I have also disabled the "Email sending enabled" field. I've tested this again and still not working when viewing the email logs in ServiceNow. Maybe it is better for me to make a duplicate of all of the required notifications I need to send for this...and make a condition on the notification that that field isnotemtpy and add the desired email address as a recipient of the notification. This seems maybe laborious, but achievable