function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DaveKempDaveKemp 

Issue with Cases and Milestones

Hi,

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){
        		parentIds.add(a.AccountId);
    		}
			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');
        	    	a.BusinessHoursId=bh.Id;
        	    	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?

Thanks,
David


 
Best Answer chosen by DaveKemp
DaveKempDaveKemp
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 :-)

David