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
davelelanddaveleland 

Apex Trigger to Populate a field on the account when the Account Owner changes

I'm trying to write my first trigger and having difficulties doing it.  I was wondering if someone could help me.
On the account record there is a field called District__c.  This needs to be the same as Territory__c on the account owner's user record.  I would like the trigger to fire when the account owner is changed to keep the two fields in sync.  The trigger would need to work also when accounts are mass transferred from one owner to the other.
Thank you in advance for your help!
Claire

Shiv ShankarShiv Shankar

Might be this helps you

 

trigger OwnerRoleTrigger on Account(before insert, before update) {
    Set<string> ownerId = new Set<string>();
    for(Account act : Trigger.New) {
        ownerId.add(act.ownerId);
    } 
    Map<id,User> idToUser = new Map<id,user>([select id,Territory__c from user where id in : ownerId]);
    for(Account act: Trigger.New){
        act.District__c = (idToUser.get(act.ownerId)).Territory__c ;
    }


}

 

SwarnasankhaSwarnasankha

Hi Claire,

 

You can try using the code snippet provided below which will work in the following scenarios:

  1. When the Account is created for the first time, it will copy the Territory value from the Account Owner to the District field on the Account.
  2. When the Account Owner is changed, it will copy the Territory value from the New Account Owner to the District field on the Account.
  3. It will also populate the Territory value from the New Account Owner to the District field on the Account when records are mass transferred.
trigger populateDistrictOnAcc on Account(before insert, before update)
{
    Set<Id> ownerIdSet = new Set<Id>();
    for(Account accRec : Trigger.New)
    {
        if(Trigger.IsInsert || (Trigger.IsUpdate && Trigger.oldMap.get(accRec.Id).OwnerId != Trigger.newMap.get(accRec.Id).OwnerId))
        {
            ownerIdSet.add(accRec.OwnerId);
        }
    }
    
    Map<Id,User> mapOfUserRecs = new Map<Id,User>([Select Id, Territory__c from User where Id IN: ownerIdSet]);
    
    for(Account accRec : Trigger.New)
    {
        if(Trigger.IsInsert || (Trigger.IsUpdate && Trigger.oldMap.get(accRec.Id).OwnerId != Trigger.newMap.get(accRec.Id).OwnerId))
        {
            if(mapOfUserRecs.get(accRec.OwnerId) != NULL)
            {
                accRec.District__c = mapOfUserRecs.get(accRec.OwnerId).Territory__c;
            }
        }
    }
}

I hope this helps. In case you have any questions, feel free to ask. 

 

davelelanddaveleland

thank you both!  I will try out these solutions today!

 

Claire

davelelanddaveleland

I've been working through the trigger trying to understand it and I think I'm just about there (this is my first trigger so I am a complete novice!) .  Now I am trying to work out how to do the test class.  Can you help me with that? Thank you so much!