• Brandon Hurley
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hi,

I have this trigger that I would like to convert to an apex class and I would like to add some code to the class.  The trigger changes contacts to the account owner id when the account owner is changed.  What I would like to add is something that also changes the contact owner when a contact is created or added to the account.

Here is the trigger I have.

trigger AlignContactownertoAccountOwner 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>();
      List<Contact> contactUpdates = new List<Contact>();
      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(Id = c.Id, OwnerId = newOwnerId);

               contactUpdates.add(updatedContact);

               }

            }

             

            }

       }
            update contactUpdates;

}

Any help would be great.

Thanks,

Michael 

I'm having a hard time writing a test method for a custom object that has Formula Fields on them that are being used in the methods I am trying to cover.

 

When I debug the fields in the test method they are coming up as null, but, if I query the record it shows those fields as being updated correctly.

 

Where I run into a problem is when I have nested formula fields and then pass the final record I want tested to the method via a trigger.  The field that starts at the top level of the hierarchy hasn't made it all of the way to the final record.

 

Is there something I'm missing or do I need to query each and every object before associating it to the next in the hierarchy?  The final record doesn't have a direct reference to the fields in question but the class queries them based on a set criteria.

 

Any help would be appreciated.  Thanks!

I have a trigger set up on an object to trigger after an update.  If I do the update manually it works just fine.  However, if I do a mass update using the Data Loader the trigger doesn't get invoked.
 
Is there a way to force the trigger while using the Apex Data Loader?
  • November 18, 2008
  • Like
  • 0