+ Start a Discussion
Nishad BashaNishad Basha 

How to Calculate the Case Age considering Salesforce Business Hours using schedule Apex?

global class ScheduledApexOnCase implements Schedulable {    
    global void execute(SchedulableContext sc) {
    Date today = system.today();
    Decimal caseAge;
    List<case> newCaseList = new List<case>();
    BusinessHours stdBusinessHours = [select id from businesshours where isDefault = true];
    newCaseList = [Select id, CreatedDate, Status, CaseAge__c from Case]; //CaseAge__c is a Text field
    for(Case eachCase: newCaseList) {        
        if(eachCase.Status != 'Closed') 
            caseAge = BusinessHours.diff(stdBusinessHours.id, eachCase.CreatedDate, today) / 1000 / 3600; 
            caseAge = BusinessHours.diff(stdBusinessHours.id, eachCase.CreatedDate, eachCase.ClosedDate) / 1000 / 3600;
        eachCase.CaseAge__c = String.valueOf(caseAge) + ' hours';        
    update newCaseList;

Here iam getting  Compile Error: Illegal assignment from String to Decimal at line 16 column 9, How to solve the above scenario please give me some ideas
CaseAge__c is a number field you can do as below :

eachCase.CaseAge__c = caseAge;

you cannot append string to a number field.
Chiel de Graaf.Chiel de Graaf.
This is an interesting question. I've got this working for quit a long time, but i want the calculation to be in minutes, rather then hours.

So if the difference is 2,5 hours, the field is filled with 150 minutes.
What do i have to change in the code to realize this ?