You need to sign in to do that
Don't have an account?

Need Help with Apex Trigger Calulcating Wait times for Departments on Cases
Hi,
I have a field called sub status which tracks the different departments who handle cases.
My trigger sums time between each status change in new custom fields on the case object (in hours). My problem is, that if a case is assigned a sub_status of Deployment and then resolve (closed) with the same sub_status, it doest count the time.
Can anyone take a quick look and see what I should change? I dont know how make sure that it sums the time between statuses when the case is closed, even if the sub_status didnt change
trigger calculateSubstatusHours on Case (before insert, before update) {
if (Trigger.isInsert) {
for (Case updatedCase:System.Trigger.new) {
updatedCase.Last_Status_Change__c = System.now();
updatedCase.Time_With_Customer__c = 0;
updatedCase.Time_With_Tier2__c = 0;
updatedCase.Time_With_Deployment__c = 0;
}
} else {
//Get the default business hours (we might need it)
BusinessHours defaultHours = [select Id from BusinessHours where IsDefault=true];
//Get the closed statuses (because at the point of this trigger Case.IsClosed won't be set yet)
Set<String> closedStatusSet = new Set<String>();
for (CaseStatus status:[Select MasterLabel From CaseStatus where IsClosed=true]) {
closedStatusSet.add(status.MasterLabel);
}
//For any case where the status is changed, recalc the business hours in the buckets
for (Case updatedCase:System.Trigger.new) {
Case oldCase = System.Trigger.oldMap.get(updatedCase.Id);
if (oldCase.Sub_Condition__c!=updatedCase.Sub_Condition__c && updatedCase.Last_Status_Change__c!=null) {
//OK, the status has changed
if (!oldCase.IsClosed) {
//We only update the buckets for open cases
//On the off-chance that the business hours on the case are null, use the default ones instead
Id hoursToUse = updatedCase.BusinessHoursId!=null?updatedCase.BusinessHoursId:defaultHours.Id;
//The diff method comes back in milliseconds, so we divide by 60000 to get minutes.
Double timeSinceLastStatus = BusinessHours.diff(hoursToUse, updatedCase.Last_Status_Change__c, System.now())/3600000.0;
System.debug(timeSinceLastStatus);
//We decide which bucket to add it to based on whether it was in a stop status before
boolean foundStatus = false;
if (oldCase.Sub_Condition__c == 'Customer - Confirmation of Fix' || oldCase.Sub_Condition__c == 'Customer - More Info')
{
updatedCase.Time_With_Customer__c += timeSinceLastStatus;
foundStatus = true;
}
if (oldCase.Sub_Condition__c == 'Deployment')
{
updatedCase.Time_With_Deployment__c += timeSinceLastStatus;
foundStatus = true;
}
if (oldCase.Sub_Condition__c == 'Support - 2nd Tier')
{
updatedCase.Time_With_Tier2__c += timeSinceLastStatus;
foundStatus = true;
}
}
updatedCase.Last_Status_Change__c = System.now();
}
}
}
}

Impossible to read your code man.

Sorry, I was using Chrome