- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2025 04:57 AM
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('active=true^cat_item=uvsdalvyut6768s8^approval=requested^sys_created_onRELATIVELT@dayofweek@ago@3');
gr.query();
while (gr.next()) {
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('active=true^cat_item=uvsdalvyut6768s8^approval=requested^sys_created_onRELATIVELT@dayofweek@ago@3');
gr.query();
while (gr.next()) {
var createdDate = gr.getValue('sys_created_on');
var createdGlideDate = new GlideDateTime(gr.sys_created_on).getLocalDate();
var getExpDate = createdGlideDate.getDate();
var curDateTime = new GlideDateTime();
var curDate = curDateTime.getLocalDate();
var forEmailsgetDays = GlideDateTime.subtract(getExpDate, curDate);
var reminderDays = forEmailsgetDays.getDayPart();
if (reminderDays > 3 && reminderDays <= 5) {
// Escalate to line manager's manager
var lineManager = gr.variables.user_id.manager;
var managerGR = new GlideRecord('sys_user');
if (managerGR.get(lineManager)) {
var managerManager = managerGR.manager;
if (!managerGR.manager.vip) {
gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
}
}
}
else if (reminderDays > 5) {
// Reject the request
gr.state = -35;
gr.stage = 'Closed Rejected';
gr.approval = 'rejected';
gr.active = 'false';
gr.comments = "Request rejected due to lack of approval within 5 days";
gr.setWorkflow(false);
gr.update();
}
Please provide additional script like it will trigger email to manager only working days irrespective all days
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2025 05:11 AM - edited ‎07-15-2025 05:16 AM
Hi @Nareshpatel,
Your script has issues with subtraction. You can try this updated script.
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('active=true^cat_item=uvsdalvyut6768s8^approval=requested^sys_created_onRELATIVELT@dayofweek@ago@3');
gr.query();
while (gr.next()) {
var createdGDT = gr.getValue('sys_created_on');
var created = new GlideDateTime(createdGDT);
var now = new GlideDateTime();
var diff = GlideDateTime.subtract(now, created);
var diffDays = Math.floor(diff.getNumericValue() / (1000 * 60 * 60 * 24));
if (diffDays > 3 && diffDays <= 5) {
// Escalate to line manager's manager
var lineManager = gr.variables.user_id.manager;
var managerGR = new GlideRecord('sys_user');
if (managerGR.get(lineManager)) {
var managerManager = managerGR.manager;
if (managerManager && !managerManager.vip) {
gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
}
}
}
else if (diffDays > 5) {
// Reject the request
gr.state = -35;
gr.stage = 'Closed Rejected';
gr.approval = 'rejected';
gr.active = 'false';
gr.comments = "Request rejected due to lack of approval within 5 days";
gr.setWorkflow(false);
gr.update();
}
}
Regards,
Ehab Pilloor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2025 05:15 AM
Hello @Nareshpatel ,
Please use code below so it will trigger email to manager only working days irrespective all days -
// Define your business calendar sys_id.
// IMPORTANT: Replace 'YOUR_BUSINESS_CALENDAR_SYS_ID' with the actual Sys ID
// of your organization's working days calendar (e.g., '8-5 weekdays').
// You can find this in System Scheduler > Schedules > Schedules.
var businessCalendarSysId = 'YOUR_BUSINESS_CALENDAR_SYS_ID';
var schedule = new GlideSchedule(businessCalendarSysId);
var currentTime = new GlideDateTime(); // Get the current date and time
// Check if the current time falls within the defined schedule (working hours and days)
if (schedule.isInSchedule(currentTime)) {
// Only send email if it's a working day AND not a VIP manager
if (!managerGR.manager.vip) {
gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
gs.log('Email sent for RITM: ' + gr.number + ' to line manager\'s manager. Reminder days: ' + reminderDays);
} else {
gs.log('Email not sent for RITM: ' + gr.number + '. Manager is VIP.');
}
} else {
gs.log('Email not sent for RITM: ' + gr.number + '. Today (' + currentTime.getDisplayValueInternal() + ') is not a working day based on calendar: ' + businessCalendarSysId);
}
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2025 05:16 AM
what's your actual business requirement?
what did you configure and where have you written the above 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-16-2025 02:00 AM
Can you provide latest script ?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2025 04:10 AM
I want to confirm the requirement again, so you want to send the notification for all days?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2025 04:30 AM
Yes, I am checking
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2025 01:19 AM
Hello @Nareshpatel ,
This looks good.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2025 02:47 AM
if i created the one RITM based on the above script i need to wait till tomorrow Friday 5 whether triggered mail or not ? i' m getting little bit confuse here pls give suggestion? or
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2025 02:52 AM
Hello @Nareshpatel ,
No, email will not trigger because you mentioned answer = false. The email will trigger only from Sunday to Thursday.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2025 02:56 AM - edited ‎07-17-2025 02:58 AM
Hi @Nareshpatel,
For debugging purpose, you can use GlideDateTime API with date inside it having present and different days of the week and pass in your script to test and see if it works in those different scenarios or not. This will give an idea which day the script will work and which day it will not.
var rightNow = new GlideDateTime('ADD_DIFFERENT_DATES_OF_A_WEEK_HERE_TO_TEST');
//var dayOfWeek = rightNow.getDayOfWeekUTC();
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
//6=Saturday, 5=Friday
if (dayOfWeek == 5 || dayOfWeek == 6) {
answer = false;
} else {
answer = true;
Regards,
Ehab Pilloor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2025 05:15 AM
Hello @Nareshpatel ,
Please use code below so it will trigger email to manager only working days irrespective all days -
// Define your business calendar sys_id.
// IMPORTANT: Replace 'YOUR_BUSINESS_CALENDAR_SYS_ID' with the actual Sys ID
// of your organization's working days calendar (e.g., '8-5 weekdays').
// You can find this in System Scheduler > Schedules > Schedules.
var businessCalendarSysId = 'YOUR_BUSINESS_CALENDAR_SYS_ID';
var schedule = new GlideSchedule(businessCalendarSysId);
var currentTime = new GlideDateTime(); // Get the current date and time
// Check if the current time falls within the defined schedule (working hours and days)
if (schedule.isInSchedule(currentTime)) {
// Only send email if it's a working day AND not a VIP manager
if (!managerGR.manager.vip) {
gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
gs.log('Email sent for RITM: ' + gr.number + ' to line manager\'s manager. Reminder days: ' + reminderDays);
} else {
gs.log('Email not sent for RITM: ' + gr.number + '. Manager is VIP.');
}
} else {
gs.log('Email not sent for RITM: ' + gr.number + '. Today (' + currentTime.getDisplayValueInternal() + ') is not a working day based on calendar: ' + businessCalendarSysId);
}
Thank you.