+ Start a Discussion
MattsmithMattsmith 

Trigger to update contact owner when account owner changes without changing owner in opportunities

I'm very new to this so any  help would be of great assistance!

I'm currently using the below trigger;
I'm trying to get the trigger to update the AccountOwnerID ONLY within "Contacts"  when the "Accounts" ownerID is changed.

Currently "Oppurtunities" is being changed as well, which defeats the purpose for us.

I'm not sure this is even possible, so any help would be greatly appreciated.
 

trigger Changeownertrigger on Account (after insert,after update) {
       
          Set<Id> accountIds = new Set<Id>();
          Map<Id, String> oldOwnerIds = new Map<Id, String>();
          Map<Id, String> newOwnerIds = new Map<Id, String>();
          Contact[] contactUpdates = new Contact[0];
          
          for (Account a : Trigger.new)
          {
             if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
             {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
                newOwnerIds.put(a.Id, a.OwnerId);
                accountIds.add(a.Id);
             }

          }
            if (!accountIds.isEmpty()) {
             for (Account acc : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])
                {
                String newOwnerId = newOwnerIds.get(act.Id);
                String oldOwnerId = oldOwnerIds.get(act.Id);
                for (Contact c : acc.Contacts)
                {
                   if (c.OwnerId == oldOwnerId)
                   {
                   Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId);
                   contactUpdates.add(updatedContact);
                   }
                }
                 
                }
           }
                update contactUpdates;
    }

James LoghryJames Loghry
Hi Mattsmiff,

Can you please repost the code above using the Code formatting "< >" button?  It's very difficult ot read your code in its current state.

Thanks.
Ketankumar PatelKetankumar Patel
Hi Mattsmiff, 

You should create trigger on opportunity and revert back your opportunity owner changes using Trigger.Old.
MattsmithMattsmith
trigger Changeownertrigger on Account (after insert,after update) {
	   
	      Set<Id> accountIds = new Set<Id>();
	      Map<Id, String> oldOwnerIds = new Map<Id, String>();
	      Map<Id, String> newOwnerIds = new Map<Id, String>();
	      Contact[] contactUpdates = new Contact[0];
	      
	      for (Account a : Trigger.new)
	      {
	         if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
	         {
	            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
	            newOwnerIds.put(a.Id, a.OwnerId);
	            accountIds.add(a.Id);
	         }

	      }
	        if (!accountIds.isEmpty()) {
	         for (Account acc : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])
	            {
	            String newOwnerId = newOwnerIds.get(act.Id);
	            String oldOwnerId = oldOwnerIds.get(act.Id);
	            for (Contact c : acc.Contacts)
	            {
	               if (c.OwnerId == oldOwnerId) 
	               {
	               Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId);
	               contactUpdates.add(updatedContact);
	               }
	            }
	             
	            }
	       }
	            update contactUpdates;
	}
Thanks guys, here is the code again
JoreeBabu KodiJoreeBabu Kodi
Hi Mattsmiff,

I just updated your code, please find the below. You can't update the Salesforce ID, so i removed that.
trigger Changeownertrigger on Account (after update) {
       
          Set<Id> accountIds = new Set<Id>();
          Map<Id, String> oldOwnerIds = new Map<Id, String>();
          Map<Id, String> newOwnerIds = new Map<Id, String>();
          Contact[] contactUpdates = new Contact[0];
          
          for (Account a : Trigger.new)
          {
             if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
             {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
                newOwnerIds.put(a.Id, a.OwnerId);
                accountIds.add(a.Id);
             }

          }
            if (!accountIds.isEmpty()) {
             for (Account acc : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])
                {
                String newOwnerId = newOwnerIds.get(acc.Id);
                String oldOwnerId = oldOwnerIds.get(acc.Id);
                for (Contact c : acc.Contacts)
                {
                   if (c.OwnerId == oldOwnerId) 
                   {
                   Contact updatedContact = new Contact(OwnerId = newOwnerId);
                   contactUpdates.add(updatedContact);
                   }
                }
                 
                }
           }
                update contactUpdates;
    } 

Is it required after insert event in your trigger? i hope this is work to you. if not can you please elaborate your requirment.

Thanks&Regards
Joree
joreekodi@gmail.com
Prabhat Gangwar 14Prabhat Gangwar 14

See this Code I have tested :

If you will change Account Owner then your contact owner will change and opportunity Owner will not change .

====================================================================================================================

 

 

trigger Changeownertrigger on Account (after update) {
map<id,id>mapOldAccountOwner= new map<id,id>();
map<id,id>mapNewAccountOwner= new map<id,id>();
List<opportunity >ListOfopportunity = new List<opportunity >();
    for(Account acc:trigger.new){
        if(acc.OwnerId != Trigger.oldMap.get(acc.Id).OwnerId){
         mapOldAccountOwner.put(acc.id,Trigger.oldMap.get(acc.Id).OwnerId); 
         mapNewAccountOwner.put(acc.id,acc.OwnerId);
        }
    }
    if(mapOldAccountOwner.size()>0){
        for (Account acc : [SELECT Id,(SELECT Id, AccountId ,OwnerId FROM Opportunities) FROM Account WHERE Id in :mapOldAccountOwner.keyset()]){
                for(opportunity opp :acc.Opportunities){
                    if(opp.OwnerId ==mapNewAccountOwner.get(opp.AccountId)){
                        opportunity opp1= new opportunity (id=opp.id,OwnerId=mapOldAccountOwner.get(opp.AccountId));
                        ListOfopportunity.add(opp1);
                    }
                }        
        }
    }
    if(ListOfopportunity.size()>0 && ListOfopportunity!= null){
       update ListOfopportunity;
    }
}

 

Edward EncarnacionEdward Encarnacion
Hey matsmiff. your code looks pretty good, noting really sticks out to me right away. What sort of issues are you experiencing? compiler issues? or just general unit test issues? If you could give details on the error then probably we can flesh out a solution?