• Mattsmith
  • NEWBIE
  • 0 Points
  • Member since 2015
  • Redback Conferencing

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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;
    }

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;
    }

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;
    }