- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 09:04 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 09:56 PM
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 -
Thank you
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 09:15 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 09:22 PM
Hello @Naman Jain2412 ,
For your reference, please find below link for sending notification to the CC -
Thank you
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 09:32 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 09:56 PM
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 -
Thank you
Thank you.