
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2024 02:36 AM
Hi All,
I need help in scripting. I have String field where I have to populate the count of days past since INC was created excluding the weekends (i.e sat & sun). I am able to achieve the count of days but need help in excluding the weekends.
--------------------------------
Using below script in Calculate value of the field.
(function calculatedFieldValue(current) {
var startDate = new GlideDateTime(current.getValue('sys_created_on'));
var endDate = new GlideDateTime();
var dayCount = GlideDateTime.subtract(startDate,endDate);
return dayCount.getDayPart();
})(current);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2024 05:33 AM
Hi @DEEPAK KUMAR SI ,
Please try the below, tested in PDI:
(function calculatedFieldValue(current) {
var startDate = new GlideDateTime(current.getValue('sys_created_on'));
var endDate = new GlideDateTime();
var diffDays = 0;
while (startDate.compareTo(endDate) < 0) {
startDate.addDays(1);
var weekDay = startDate.getDayOfWeek();
if (weekDay != 6 && weekDay != 7) {
diffDays++;
}
}
return diffDays;
})(current);
Output:
Mark this as Helpful / Accept the Solution if this helps
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2024 02:59 AM
Hello @DEEPAK KUMAR SI ,
Please use the GlideSchedule API for this use case.
Please find the below link which will help you to script it.
Thank you,
Omkar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2024 04:23 AM
Using this schedule
and below is script.. now i am getting 0 as value in the field.
(function calculatedFieldValue(current) {
// Add your code here
var sd = new GlideDateTime(current.getValue('sys_created_on'));
var ed = new GlideDateTime();
var schedule = new GlideSchedule();
schedule.load('d2fcb99887074e10b2a165380cbb352f');
var dur = schedule.duration(sd,ed);
return dur.getDayPart();
})(current);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2024 05:33 AM
Hi @DEEPAK KUMAR SI ,
Please try the below, tested in PDI:
(function calculatedFieldValue(current) {
var startDate = new GlideDateTime(current.getValue('sys_created_on'));
var endDate = new GlideDateTime();
var diffDays = 0;
while (startDate.compareTo(endDate) < 0) {
startDate.addDays(1);
var weekDay = startDate.getDayOfWeek();
if (weekDay != 6 && weekDay != 7) {
diffDays++;
}
}
return diffDays;
})(current);
Output:
Mark this as Helpful / Accept the Solution if this helps
Mark this as Helpful / Accept the Solution if this helps.