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
Michael Lindsay 4Michael Lindsay 4 

Convert Trigger to Apex class

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 
Best Answer chosen by Michael Lindsay 4
GauravGargGauravGarg
Hi Michael,

Please find below trigger and class code:
Trigger:
 
trigger AlignContactownertoAccountOwner on Account (after insert,after update) {

	AlignContactownertoAccountOwner_AC(trigger.New, trigger.oldMap);

}

Class
public class AlignContactownertoAccountOwner_AC{

	public static AlignContactownertoAccountOwner(List<Account> newList, Map<Id, Account>oldMap)
	{
	  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 : newList)
	  {
		 if (a.OwnerId != oldMap.get(a.Id).OwnerId)
		 {
			oldOwnerIds.put(a.Id, 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;
	}
}



Thanks,

Gaurav
Skype: gaurav62990

All Answers

GauravGargGauravGarg
Hi Michael,

Please find below trigger and class code:
Trigger:
 
trigger AlignContactownertoAccountOwner on Account (after insert,after update) {

	AlignContactownertoAccountOwner_AC(trigger.New, trigger.oldMap);

}

Class
public class AlignContactownertoAccountOwner_AC{

	public static AlignContactownertoAccountOwner(List<Account> newList, Map<Id, Account>oldMap)
	{
	  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 : newList)
	  {
		 if (a.OwnerId != oldMap.get(a.Id).OwnerId)
		 {
			oldOwnerIds.put(a.Id, 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;
	}
}



Thanks,

Gaurav
Skype: gaurav62990

This was selected as the best answer
Michael Lindsay 4Michael Lindsay 4
Gaurav, 

Getting an error.

Error: Compile Error: static is not allowed on constructors at line 2 column 15

Does not like the 'public static' in line 2.

Thanks,

Michael
GauravGargGauravGarg
Michael,

Please update with 
public static void AlignContactownertoAccountOwner

Thanks,
Gaurav​

 
Michael Lindsay 4Michael Lindsay 4
Gaurav,
Thank you....it is working now.
Michael 
Brandon HurleyBrandon Hurley
The code above didn't work for me. I kept getting an error on the trigger. I am no developer, and I read a few pages of a book on Java one night, so let me preface this with that, but I solved the error by doing the following to cast trigger.new into a class.

For the trigger, you may have to specify both the class and method, not just the class.
trigger CustomObject__cTrigger on CustomObject__c(before insert){
	CustomObjectInsertClass.CustomObjectInsertMethod(trigger.new);
}

And then you just change all your "for loops" or anthing else that references trigger.new to whatever you're calling the list in the class.
Instead of:
for (CustomObject__c A : trigger.new){
//your code here
}

you'll write:
public class CustomObjectInsertClass {
    public static void CustomObjectInsertMethod(List<CustomObject__c> newList){
        for (CustomObject__c A : newList){
        //your code here
        }
    }
}
//so newList  now contains trigger.new
//yay!


And if you need to reference the old map on an update to see if something was null or something like that, then what you cast in the trigger you receive in the class in the same order.
//trigger
trigger CustomObject__cTrigger on CustomObject__c(before update){
    CustomObjectInsertClass.CustomObjectInsertMethod(trigger.new, trigger.oldMap);
}
 
//class
public class CustomObjectInsertClass {
    public static void CustomObjectInsertMethod(List<CustomObject__c> newList, Map(Id,CustomObject__c) oldMap){
        for (CustomObject__c A : newList){
        //your code here
        }
    }
}
//and now oldMap can be referenced in the same way as did inside the old trigger code.
//you would not believe how long it took me to realize the order you cast them in the trigger correspons to the order in which you receive / declare them in the class. Knowing is half the battle.

Anyway. I hope this helps. And i hope this is correct. It works in my org.