0

I'm trying to create a trigger that will update a field on an account whenever the ownership of the account changes. I've tried to remove the NullPointerException error by adding if statements, but I'm still getting the same error and not sure what is going wrong. Any help would be fantastic!

trigger BusinessDivisionAccountOwnership on Account (after update) 
{

    Set<Id> relatedAccountIdSet = new Set<Id>();

    for (Account acct : Trigger.new)
    {
        if (acct.Id != null)
        {
            relatedAccountIdSet.add(acct.Id);
        }
    }

    Map<Id, User> relatedAccountsMap = new Map<Id, User>([SELECT Id
                                                               , Default_Business_Division__c
                                                           FROM User
                                                           WHERE Id IN :relatedAccountIdSet]);
    for (Account newAcct : Trigger.new)
    {
        Account oldAcct = Trigger.oldMap.get(newAcct.Id);
        if (oldAcct.ownerId != null)
        {
        if ((oldAcct.OwnerId != newAcct.OwnerId) && (newAcct.OwnerId != '00560000001NPSe') && (oldAcct.OwnerId != null) && (newAcct.OwnerId != null))
        {
            User newOwner = relatedAccountsMap.get(newAcct.OwnerId);
            if (newOwner.Default_Business_Division__c != null)
            {
                newAcct.Business_Division__c = newOwner.Default_Business_Division__c;
            }
        }
        }

    }

}

Business_Division__c is the field I want to change on the account object, Default_Business_Division__c is a field on the account owner's page that I want it to pull from. I've also added logic to the trigger to prevent this change from occurring if our API user gains ownership.

  • In your relatedAccountIdSet why are you putting Account.Id and not the Account.OwnerId and at what line you have the error? – melmoussaoui Apr 24 '14 at 16:16
  • 1
    And why don't you do this just by creating a formula field on Account which references Account.Owner.Default_Business_Division__c – melmoussaoui Apr 24 '14 at 16:20
  • The final line of code is throwing the error, newAcct.Business_Division__c = newOwner.Default_Business_Division__c; I was using Account.Id to determine which accounts need to be updated, as not all accounts that are owned by the account owner should be updated, just accounts who change account owners. I cannot create a formula field for this, because there are cases where management will want to change the field to something else independent of the account owner's field. – grimsvotn Apr 24 '14 at 16:20
1

You are adding the AccountId to the Set at the start instead of the OwnerId. This caused no users to come back in your query.

relatedAccountIdSet.add(acct.Id);

to

relatedAccountIdSet.add(acct.OwnerId);

Btw, this should be a before update trigger. Not after update.

  • Thank you, this fixed it. Would you mind explaining the difference between before and after update? I figured it would be an after update trigger, because we want the value of the new owner, and the new owner wouldn't be on the account until after you updated? – grimsvotn Apr 24 '14 at 16:41
  • 1
    That's why there is a Trigger.new and Trigger.old. The Trigger.new refers to the new value. What before update means is before saving to the database. That means you want to change what a field will be before you actually save it in the database. In the after update the value has been fully saved and committed to the database already and so you can't change the value. – dphil Apr 24 '14 at 17:35

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.