0

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;
    }
  • 1
    Which trigger event are you using? Before Insert? – Michael Gill Oct 8 '12 at 15:54
  • before insert, before update – jackerman09 Oct 8 '12 at 15:58
  • It may help to show in the code sample how newFinAccts is set (the comment indicates it comes from trigger.new) – Stephen Willcock Oct 8 '12 at 16:31
2

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()]

  • I didn't fully follow your answer, but are you saying that I need to use an SOQL query on NewFinAccts to pull client__c? – jackerman09 Oct 8 '12 at 16:02
  • newFinAccts=[Select Id,client__c,client__r.Wealth_Advisor__c from Financial_Account__c where id in Trigger.NewMap.keyset()] – Mohith Shrivastava Oct 8 '12 at 16:06
  • Use the above before and you will get in debug logs – Mohith Shrivastava Oct 8 '12 at 16:07
  • Thanks! This worked, but I am now getting another error. The trigger saves, but when I try to test it by updating a Financial Account, I get this error: – jackerman09 Oct 8 '12 at 16:20
  • Error:Apex trigger transferRelationshipTeamFromAcct caused an unexpected exception, contact your administrator: transferRelationshipTeamFromAcct: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a00L0000000wgatIAA; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a00L0000000wgat) is currently in trigger transferRelationshipTeamFromAcct, therefore it cannot recursively update itself: []: Class.FinancialAccountManager.handleTransferRelationshipTeamFromAcct: line 55, column 1 – jackerman09 Oct 8 '12 at 16:20
4

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/

  • Thanks, I expected finAcct.Wealth_Advisor_Lookup__c to be null, I was surprised by the fact that client__r.Wealth_Advisor__c is null, because that was already populated. – jackerman09 Oct 8 '12 at 16:15
  • Primary references will be valid, as long as they have a value. so finAcct.Wealth_Advisor_Lookup__c would be not null if it as a value, but finAcct.Wealth_Advisor_Lookup__r.XXX will be null even if it has a value. – techtrekker Oct 8 '12 at 16:19
  • why a down vote ? whats incorrect !? – techtrekker Oct 8 '12 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.