The CreatorCon Call for Content is officially open! Get started here.

CC Recipients based on field Value through Email Script

Naman Jain2412
Tera Expert

Hi,

 

I have created a email notification which consists of Scheduled Job , Notification and Email Script. All are working fine however when I have tried to add the recipients through field in CC, its not showing recipients in CC after triggering the Notification. I have added the code for reference. pls let me the resolution of the same.

 

 

Email Script

 

 var gr = new GlideRecord("sn_compliance_policy");
    gr.addQuery("sys_id", current.sys_id);
    gr.query();
    if (gr.next())

{
 
    email.addAddress('cc', 'u_corp_cert_spoc_to_ensure_document_maintenance');
    email.addAddress('cc', 'u_custodian_l4_name.email');
    // Add the code here
   
   
   
    //email.addAddress('cc',"u_custodian_l4_name.email");
    // var user = new GlideRecord("sys_user");
    // user.addQuery("name",)
    // email.addAddress('cc', gr,gr.u_custodian_l4_name.email.toString(), u_custodian_l4_name.getDisplayValue());
}

})(current, template, email, email_action, event);
 
 
Another Use Case including in this to trigger the notification every alternate date after renewal date will complete
 
Here is the Script of Scheduled Job
Please let me know is this the correct script based on the given scenario and enhance this script as per criteria.
 
var user;
var gr = new GlideRecord('sn_compliance_policy');
gr.addQuery("state", "draft");
gr.query();
while (gr.next()) {


    var u = new GlideDateTime(gr.u_next_renewal_date);
    var c = new GlideDateTime().getDate();
    var d = gs.dateDiff(c, u, true);
    var days = d / (60 * 60 * 24);



    if (days == 15 || days == 14 || days == 12 || days == 10 || days == 8 || days == 6 || days == 4 || days == 2 || days == 0) {
        gs.eventQueue('sn_event_renewal_alternate_policy', gr, gr.getDisplayValue('u_custodian_l4_name'), gr.getDisplayValue('u_custodian_name'));
        // gs.log("900 completed");
    }

}
 
1 ACCEPTED SOLUTION

Shraddha Kadam
Mega Sage

@Naman Jain2412 ,

 

For your second use case, use the code below. I have tried it on a background script and it worked.

// This script sends a notification on every alternate day
// after a record's renewal date has passed, without using system properties for tracking.

// --- Configuration ---
var TABLE_NAME = 'incident';        // **IMPORTANT: Replace with your actual table name (e.g., 'alm_asset', 'u_my_custom_table')**
var RENEWAL_DATE_FIELD = 'u_renewal_date';  // **IMPORTANT: Replace with the actual field name for your renewal date (must be a Date or Date/Time field)**

// --- Main Script printic ---
var gr = new GlideRecord(TABLE_NAME);
// Query for active records where the renewal date is today or in the past
gr.addEncodedQuery('u_renewal_date!=NULL^number=INC0009009');  // add your encoded query
gr.query();
var todayGDT = new GlideDateTime();
var todayDateString = todayGDT.getLocalDate().getValue(); // Get today's date in YYYY-MM-DD format

while (gr.next()) {
    var recordSysId = gr.sys_id.toString();
    var recordDisplayValue = gr.getDisplayValue(); // Or gr.number, gr.name, etc. depending on your table

    // Get the renewal date from the record
    var renewalDateGDT = new GlideDateTime(gr.getValue(RENEWAL_DATE_FIELD));
    var renewalDateString = renewalDateGDT.getLocalDate().getValue(); // Get renewal date in YYYY-MM-DD format

    // Calculate the difference in days between renewal date and today
    // gs.dateDiff(date1, date2, asSeconds) returns difference in seconds
    var diffSeconds = gs.dateDiff(renewalDateString, todayDateString, true);
    var daysDifference = Math.floor(diffSeconds / (60 * 60 * 24)); // Convert seconds to full days

    gs.print('Processing record: ' + recordDisplayValue + ') - Renewal Date: ' + renewalDateString + ', Today: ' + todayDateString + ', Days Difference: ' + daysDifference);

    // Condition to send notification:
    // 1. daysDifference must be greater than 0 (i.e., renewal date has passed)
    // 2. daysDifference must be an odd number (1, 3, 5, etc.) for alternate days
    if (daysDifference > 0 && (daysDifference % 2) === 1) {
        gs.print('Notification for ' + recordDisplayValue + ': Sending today as it is an alternate day after renewal.');
            //gs.eventQueue(NOTIFICATION_NAME, gr, recipientSysId, '');
        
    } else {
        gs.print('Notification for ' + recordDisplayValue + ': Not an alternate day after renewal or renewal date not passed. Skipping.');
    }
}

gs.print('Alternate Day Post-Renewal Notification Script finished.');

 

Output -

ShraddhaKadam_0-1751518569366.png

 

Thank you

 

 

If my response was helpful, please mark it as correct and helpful.
Thank you.

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Naman Jain2412 

syntax for adding cc is this

email.addAddress('cc', <email>, <name>);

share complete script

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

Shraddha Kadam
Mega Sage

Hello @Naman Jain2412 ,

 

For your reference, please find below link for sending notification to the CC -

https://www.servicenow.com/community/servicenow-ai-platform-forum/how-to-set-cc-with-email-content/m...

 

Thank you

If my response was helpful, please mark it as correct and helpful.
Thank you.

SanjanaK
Tera Expert

Hey @Naman Jain2412 ,

 

You can try using

 

//gs.log('Check email: ' + gr.u_custodian_l4_name.email.toString()):

email.addAddress('cc', gr.u_custodian_l4_name.email.toString());

 

*********************************************************************

Hope the answer helps!

Happy Learning!

 

 

Shraddha Kadam
Mega Sage

@Naman Jain2412 ,

 

For your second use case, use the code below. I have tried it on a background script and it worked.

// This script sends a notification on every alternate day
// after a record's renewal date has passed, without using system properties for tracking.

// --- Configuration ---
var TABLE_NAME = 'incident';        // **IMPORTANT: Replace with your actual table name (e.g., 'alm_asset', 'u_my_custom_table')**
var RENEWAL_DATE_FIELD = 'u_renewal_date';  // **IMPORTANT: Replace with the actual field name for your renewal date (must be a Date or Date/Time field)**

// --- Main Script printic ---
var gr = new GlideRecord(TABLE_NAME);
// Query for active records where the renewal date is today or in the past
gr.addEncodedQuery('u_renewal_date!=NULL^number=INC0009009');  // add your encoded query
gr.query();
var todayGDT = new GlideDateTime();
var todayDateString = todayGDT.getLocalDate().getValue(); // Get today's date in YYYY-MM-DD format

while (gr.next()) {
    var recordSysId = gr.sys_id.toString();
    var recordDisplayValue = gr.getDisplayValue(); // Or gr.number, gr.name, etc. depending on your table

    // Get the renewal date from the record
    var renewalDateGDT = new GlideDateTime(gr.getValue(RENEWAL_DATE_FIELD));
    var renewalDateString = renewalDateGDT.getLocalDate().getValue(); // Get renewal date in YYYY-MM-DD format

    // Calculate the difference in days between renewal date and today
    // gs.dateDiff(date1, date2, asSeconds) returns difference in seconds
    var diffSeconds = gs.dateDiff(renewalDateString, todayDateString, true);
    var daysDifference = Math.floor(diffSeconds / (60 * 60 * 24)); // Convert seconds to full days

    gs.print('Processing record: ' + recordDisplayValue + ') - Renewal Date: ' + renewalDateString + ', Today: ' + todayDateString + ', Days Difference: ' + daysDifference);

    // Condition to send notification:
    // 1. daysDifference must be greater than 0 (i.e., renewal date has passed)
    // 2. daysDifference must be an odd number (1, 3, 5, etc.) for alternate days
    if (daysDifference > 0 && (daysDifference % 2) === 1) {
        gs.print('Notification for ' + recordDisplayValue + ': Sending today as it is an alternate day after renewal.');
            //gs.eventQueue(NOTIFICATION_NAME, gr, recipientSysId, '');
        
    } else {
        gs.print('Notification for ' + recordDisplayValue + ': Not an alternate day after renewal or renewal date not passed. Skipping.');
    }
}

gs.print('Alternate Day Post-Renewal Notification Script finished.');

 

Output -

ShraddhaKadam_0-1751518569366.png

 

Thank you

 

 

If my response was helpful, please mark it as correct and helpful.
Thank you.