Our business has a requirement to calculate a due date on a case by using the created date and adding business hours, or business days. When we calculate business days it converts the days to 24 hours and divides the result by the business hours(10 hours) which is giving us wrong due dates.
Example:
If our created date is today(8/5/16) and we add 5 business days it converts it to 120 hours. It then divides the hours by the business hours which is 12 business days and adds that to the created date which is incorrect.
Is there anyway to make the days convert to amount of time in a business day(which could be dynamic based on BusinessHours) instead of 5 business days being 5 24 hours days?
Heres the code we have currently:
Long interval = c.CreatedDate.addDays(Integer.valueOf(amountOfTimeString)).getTime() - c.CreatedDate.getTime();
c.SLA_Due_Date__c = BusinessHours.add(bh.Id, c.CreatedDate, interval);
I would wish to avoid dividing the interval by a magic number and have it instead be dynamic by the BusinessHours.
Any help would be great
BusinessHours API:
Edit: adding complete method of snippet
private static Boolean setDate(Object amountOfTime, Object timeType, Case c, BusinessHours bh, SObject referenceObject) {
Decimal amountOfTimeString = (Decimal) amountOfTime;
String timeTypeString = (String) timeType;
if(!String.isEmpty(String.valueOf(amountOfTimeString))) {
if(timeTypeString == 'Business Hours') {
Long interval = c.CreatedDate.addHours(Integer.valueOf(amountOfTimeString)).getTime() - c.CreatedDate.getTime();
c.SLA_Due_Date__c = BusinessHours.add(bh.Id, c.CreatedDate, interval);
} else if(timeTypeString == 'Calendar Hours') {
c.SLA_Due_Date__c = c.CreatedDate.addHours(Integer.valueOf(amountOfTimeString));
} else if(timeTypeString == 'Business Days') {
System.debug('Amount of time string: ' + amountOfTimeString);
Long interval = c.CreatedDate.addDays(Integer.valueOf(amountOfTimeString)).getTime() - c.CreatedDate.getTime();
System.debug('Created Date: ' + c.CreatedDate);
c.SLA_Due_Date__c = BusinessHours.add(bh.Id, c.CreatedDate, interval);
} else if(timeTypeString == 'Calendar Days') {
c.SLA_Due_Date__c = c.CreatedDate.addDays(Integer.valueOf(amountOfTimeString));
}
return true;
}
return false;
}
BusinessHours.add
. Just useInteger.valueOf(amountOfTimeString)
instead of what you have.