Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 05:27 AM
try this
(function executeRule(current, previous /*null when async*/) {
// Get the Requested Item (RITM) record
var ritmRecord = current.request_item.getRefRecord();
// Access the variable 'project_category' from the RITM
var variableValue = ritmRecord.variables.project_category;
// If value is 'Standard', set due date to 5 business (week) days later
if (variableValue == 'Standard') {
var businessDaysToAdd = 5;
var daysAdded = 0;
var gdt = new GlideDateTime();
// Start with today's date and time
var startDate = new GlideDateTime();
gdt.setValue(startDate.getValue()); // Retain current date and time
while (daysAdded < businessDaysToAdd) {
gdt.addDaysUTC(1); // Add 1 calendar day at a time
// Get the date portion in UTC and determine the weekday
var gd = new GlideDate();
gd.setValue(gdt.getDate()); // getDate() gives UTC date portion
var dayOfWeek = gd.getDayOfWeek(); // 1 = Sunday, 7 = Saturday (UTC-based)
// Skip weekends (Saturday = 6, Sunday = 7), count only weekdays
if (dayOfWeek != 6 && dayOfWeek != 7) {
daysAdded++;
}
}
// Set the due date in the current record
current.due_date = gdt;
}
})(current, previous);
It worked for me in background scripts
var businessDaysToAdd = 5;
var daysAdded = 0;
var gdt = new GlideDateTime();
gs.info('now time' + gdt);
// Start with today's date and time
var startDate = new GlideDateTime();
gdt.setValue(startDate.getValue()); // Retain current date and time
while (daysAdded < businessDaysToAdd) {
gdt.addDaysUTC(1); // Add 1 calendar day at a time
// Get the date portion in UTC and determine the weekday
var gd = new GlideDate();
gd.setValue(gdt.getDate()); // getDate() gives UTC date portion
var dayOfWeek = gd.getDayOfWeek(); // 1 = Sunday, 7 = Saturday (UTC-based)
// Skip weekends (Saturday = 6, Sunday = 7), count only weekdays
if (dayOfWeek != 6 && dayOfWeek != 7) {
daysAdded++;
}
}
gs.info('Final date' + gdt);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader