I have written the following code . But it's not giving result nor any error. My requirement is to add 3 days to CreatedDate
, excluding weekend and holidays.
Datetime createdDate = system.now();
Datetime dueDate = createdDate.addDays(3);
Integer holidaysCount = 0;
BusinessHours bh;
List<BusinessHours> temp = [SELECT ID, Name, IsDefault, IsActive From BusinessHours
WHERE IsDefault = true ];
for (BusinessHours b : temp) {
if (b.IsDefault) {
bh = b;
}
}
while (createdDate != dueDate) {
Datetime now = Datetime.newInstance(createdDate.year(), createdDate.month(), createdDate.day(), 0, 0, 0);
Boolean isHoliday = !BusinessHours.isWithin(bh.Id, now);
if ((isHoliday) || createdDate.format('E') == 'Sat' || createdDate.format('E') == 'Sun') {
createdDate = createdDate.addDays(1);
}
}