I have a field called Account_Manager__c on my Account record and I am trying to set it equal to the Account Owner when certain rules are met. I got the concept to work when I was just updating a text field but I'm getting a NullPointerException when I try to update the user lookup field.

Here is my code:

trigger updateAM on Account (before update) {    
    for (Account acct: trigger.new){  
        if(acct.Owner_AM__c == 0){   
            if(acct.Owner_Role__c == 'Affiliate Team'){   
                acct.Account_Manager__r.Id = acct.Owner.Id;   

2 Answers 2


__r refers to the relationship in a lookup or master-detail relationship. Use the field name, in this case, if you want to write a new value to it. Use Account_Manager__r if you want to read fields on that related object. Furthermore, you want to use account.OwnerId and not account.Owner.Id.

Try account.Account_Manager__c = account.OwnerId;

  • Thanks Matt, it worked! The only thing changed is instead of putting "account." I used "acct." since thats what I named my account object in the 2nd row. Commented Feb 9, 2016 at 16:25

Although @MattThomas has the correct answer, the reason for your null pointer exception is a common misconception about what is in a trigger's context.

When a trigger is invoked, the trigger code only has access to the fields on the SObject. No parent fields, no children fields

Hence, acct.Account_Manager__r.Id throws the NPE because the relationship account__r is not available to the trigger.

Note this is different when using formula fields where the relationships do exist and can be used; same for workflow and process flow conditional expressions

If one needs in a trigger the value of parent or children fields, they have to be queried for in the trigger.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .