- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 09:50 AM
Hi,
I need to set due date based on working days only(5 working days)
If it's saturday(6) or sunday(7) i need to move the date to monday. I mean i need to skip the weekends and count only the working days to resolve an incident to set the due date.
I have written a business rule:
REF::
var days = 0 ;
var createdDate = new GlideDateTime(current.sys_created_on);
gs.log("createdDate is: " + createdDate);
var getSlaDays = current.u_sla_days;
gs.log("getSlaDays is: " + getSlaDays);
var dueDate;
var splitSlaDays = getSlaDays.split(" ");
gs.log("splitSlaDays is: " + splitSlaDays);
var daysValue = splitSlaDays[0];
gs.log("daysValue is: " + daysValue);
//dueDate = createdDate + daysValue;
//dueDate = createdDate.addDaysUTC(daysValue);
if(createdDate.getDayOfWeek() != 6 && createdDate.getDayOfWeek() != 7)
{ //exclude weekends
days++;
gs.log("days is: " + days);
}
createdDate.addDays(1) ;
createdDate.addDaysUTC(daysValue);
gs.log("createdDate is: " + createdDate);
dueDate = createdDate + days;
gs.log("dueDate increment is: " + dueDate);
if( dueDate.getDayOfWeek() == 6)
{
dueDate = dueDate + 2 ;
gs.log("Inside if 6: " + dueDate);
}
if ( dueDate.getDayOfWeek() == 7)
{
dueDate = dueDate + 1 ;
gs.log("Inside if 7: " + dueDate);
}
gs.log("current.u_due_date is : " + dueDate);
current.u_due_date = dueDate;
If the sladays is 2 days to resolve a ticket and the ticket is created on 24/10/19 i need to get the output as 28/10/19 to be set on the due date.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 10:42 AM
Hi Hemanth,
So after days from daysValue to the created date if the day is saturday or weekend then get the coming monday as the date?
use below script
var createdDate = new GlideDateTime(current.sys_created_on);
gs.log("createdDate is: " + createdDate);
var getSlaDays = current.u_sla_days;
gs.log("getSlaDays is: " + getSlaDays);
var dueDate;
var splitSlaDays = getSlaDays.split(" ");
gs.log("splitSlaDays is: " + splitSlaDays);
var daysValue = splitSlaDays[0];
gs.log("daysValue is: " + daysValue);
// add days to created date
createdDate.addDaysUTC(daysValue);
// if after adding days it comes as saturday i.e. day as 6 so add 2 days so that it comes to Monday
// if after adding days it comes as sunday i.e. day as 7 add 1 day so that it comes to Monday
if(createdDate.getDayOfWeekUTC() == 6)
createdDate.addDaysLocalTime(2);
else if(createdDate.getDayOfWeekUTC() == 7)
createdDate.addDaysLocalTime(1);
current.u_due_date = createdDate;
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 10:42 AM
Hi Hemanth,
So after days from daysValue to the created date if the day is saturday or weekend then get the coming monday as the date?
use below script
var createdDate = new GlideDateTime(current.sys_created_on);
gs.log("createdDate is: " + createdDate);
var getSlaDays = current.u_sla_days;
gs.log("getSlaDays is: " + getSlaDays);
var dueDate;
var splitSlaDays = getSlaDays.split(" ");
gs.log("splitSlaDays is: " + splitSlaDays);
var daysValue = splitSlaDays[0];
gs.log("daysValue is: " + daysValue);
// add days to created date
createdDate.addDaysUTC(daysValue);
// if after adding days it comes as saturday i.e. day as 6 so add 2 days so that it comes to Monday
// if after adding days it comes as sunday i.e. day as 7 add 1 day so that it comes to Monday
if(createdDate.getDayOfWeekUTC() == 6)
createdDate.addDaysLocalTime(2);
else if(createdDate.getDayOfWeekUTC() == 7)
createdDate.addDaysLocalTime(1);
current.u_due_date = createdDate;
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 09:20 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2021 09:17 PM
Hope you are doing good.
Let me know if I have answered your question.
If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2020 03:24 PM
Hello Ankur,
I have a similar situation where a business rule does a rolling date from a specific start date. The future dates should exclude weekends.
(function executeRule(current, previous /*null when async*/) {
var loop_max = current.u_service_request_count;//occurence field must be integer
var end_date = current.u_service_end_date;
var rollingDate = new GlideDateTime(current.u_last_service_review);
for (var i = 0; i <= loop_max; i++) {
createENG();
}
function createENG() {
var engagement = new GlideRecord('interaction');
engagement.assigned_to = current.u_te_primary.getDisplayValue();
rollingDate.addMonthsUTC(3); //Add 3 months to rolling date
engagement.setValue('u_due_date', rollingDate);
if (rollingDate < end_date)
engagement.insert();
}
})(current, previous);
I think I need to put this somewhere, but not sure:
if(rollingDate.getDayOfWeekUTC() == 6)
rollingDate.addDaysLocalTime(2);
else if(rollingDate.getDayOfWeekUTC() == 7)
rollingDate.addDaysLocalTime(1);
Best regards,
Desmo