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 

Can I access parent Account info from a Case before Insert trigger?

Hi,

I have a trigger that updates a Case's BusinessHours field based on the Country of the Account, if I update the Business Hours field in an After Insert trigger, then the field is updated but the Milestones (which are created on Insert) refer to the default Business Hours, not unreasonably!

Is it possible to get the country field from the parent Account in a Before Insert Trigger, if so, how would I do that?

Many thanks,
David
Best Answer chosen by DaveKemp
KaranrajKaranraj
David - Try the below code to get the lookup field values in the before insert operation
trigger MyTrigger on Case(before insert) {
    
    Set<Id> AccountIds = new Set<Id>();    
    for (Case cs: Trigger.new) {
        if (cs.AccountId != null) {
            AccountIds.add(cs.AccountId);
        }
    }
    Map<Id, AccountId> accountEntries = new Map<Id, Account>(
        [select Id,Name from Account where id in :AccountIds]
    );
for (Case caseRec : Trigger.new) {
  String accountName = accountEntries.get(caseRec .AccountId).Name; //Getting Account name, similarly you can get other field value as well 
        //your logic
    }
}

 

All Answers

KaranrajKaranraj
David - Try the below code to get the lookup field values in the before insert operation
trigger MyTrigger on Case(before insert) {
    
    Set<Id> AccountIds = new Set<Id>();    
    for (Case cs: Trigger.new) {
        if (cs.AccountId != null) {
            AccountIds.add(cs.AccountId);
        }
    }
    Map<Id, AccountId> accountEntries = new Map<Id, Account>(
        [select Id,Name from Account where id in :AccountIds]
    );
for (Case caseRec : Trigger.new) {
  String accountName = accountEntries.get(caseRec .AccountId).Name; //Getting Account name, similarly you can get other field value as well 
        //your logic
    }
}

 
This was selected as the best answer
DaveKempDaveKemp
Hi,

Thanks for that, as ever, I found something similar just after posting the question...

I'm updating the Business Hours based on the relevant Business Hours Id, but the Milestones don't seem to be using the correct Business Hours, am I missing something?
 
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);
        		}
    		}

All the above seems to indicate that it will use the Japanese Business Hours, but it's still setting the Milestones (Target Date) using the Default Business Hours
Patcs_1Patcs_1
Hi Dave

can you please paste the full code or the query for bh.

Thanks