+ Start a Discussion

Issue with Cases and Milestones


I have the following code that is called by the Before Insert Trigger on a Case, this should update the Business Hours depending on the country that the parent account is in before a new case is added to the database:
public static void setBusinessHoursForJapanPreInsert(List<Case> newCases) {
		List<Case> newCasesToUpsert = new List<Case>();
		BusinessHours bh;
        try {          	
			//Get the Id for Japanese Business Hours 
			bh = [SELECT Id, Name FROM BusinessHours WHERE Name='Japan' AND IsActive=true LIMIT 1];

			Set<id>parentIds = new Set<id>();
    		for(Case a:newCases){
			Map<id,Account> acc = new Map<id,Account>([SELECT id,Country__c FROM Account WHERE id IN :parentIds]); 			
		    for(case a:newCases){
		    	System.debug('Business hours: '+bh+' Account Country:'+acc.get(a.Accountid).Country__c);
    	    	if(acc.get(a.Accountid) != null && acc.get(a.Accountid).Country__c == 'Japan'){
    	    		System.debug('About to update case with Japanese Business Hours');
        	    	System.debug('Business Hours Updated: '+a.BusinessHoursId);

            System.debug('Cases updated prior to saving: '+newCases);        
and indeed it appears to do just that.

However, the Milestones created are always associated with the Default Business Hours even if the parent account is in Japan.

I thought the Before Insert Trigger would update the Business Hours before Inserting the Case into the system and therefore the Milestones would be created with the Japanese business hours set where appropriate.

Am i missing something here?


Best Answer chosen by DaveKemp
So, it turns out I was missing something here:

"Business hours on a case are automatically set to your organization's default business hours, unless the case matches the criteria on an escalation rule associated with different business hours."

So I've created some escalation rules and it's now updating the Milestones correctly.

Just in case anyone else experiences the same problem :-)