Financial Account is a child of Client in a Master-Detail relationship. The client (John Sandbox) has a Wealth Advisor, 'John Smith', on his client record, however, the system debug that pulls finAcct.client__r.Wealth_Advisor__c shows this field as 'null'.
Does anyone have any idea why I am not getting the value in this field? The goal of this trigger is to make sure that the lookup fields for the Client Team (one of which is 'Wealth Advisor') always match between the client record and the financial account record when they are linked. Thanks!
Boolean UpdateMe;
List <Financial_Account__c> FinAcctsToUpdate = new List<Financial_Account__c>();
//Loop through all Financial Accounts in Trigger.new
for(Financial_Account__c finAcct : newFinAccts){
UpdateMe = false;
System.debug('finAcct WA Lookup: ' + finAcct.Wealth_Advisor_Lookup__c);
System.debug('Client: ' + finAcct.client__c);
System.debug('Acct WA: ' + finAcct.client__r.Wealth_Advisor__c);
if (finAcct.Wealth_Advisor_Lookup__c != finAcct.client__r.Wealth_Advisor__c){
finAcct.Wealth_Advisor_Lookup__c = finAcct.client__r.Wealth_Advisor__c;
UpdateMe = true;
}
if(UpdateMe == true){
FinAcctsToUpdate.add(finAcct);
}
UpdateMe = false;
}
if(FinAcctsToUpdate.size() > 0 ){
update FinAcctsToUpdate;
}
Attribution to: jackerman09
Possible Suggestion/Solution #1
I guess since its a relationship, you should check whether the fields you are trying to use have been queried or not?
newFinAccts .Please see this list must be populated by query and check whether you have queried finAcct.client__r.Wealth_Advisor__c
If you are only looping through Trigger.new, let me confirm that you must query fields on the parent relationship before they can be used.
Use the following code
newFinAccts=[Select Id,client_c,client_r.Wealth_Advisor__c from Financial_Account__c where id in Trigger.NewMap.keyset()]
Attribution to: Mohith Shrivastava
Possible Suggestion/Solution #2
In a before insert trigger, relational references will evaluate to null. So finAcct.Wealth_Advisor_Lookup__r.XXX in a before trigger will be null. http://cloudnow.wordpress.com/2011/04/04/apex-bloopers-why-art-thou-null/
Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2025