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
ChickenOrBeefChickenOrBeef 

Converting trigger to class, need to change "trigger.oldMap"

Greetings everyone,

I'm in the process of converting all of my triggers to classes (and then calling them in a main trigger for each object).

Anywho, I'm trying to convert an Account trigger that transfers an account's contacts to the current account owner, but I'm getting an error with the line that references "trigger.oldMap". What do I have to do in these instances going forward? Please see the trigger and error message below.

Thanks!

User-added image

User-added image


Let me know if you need any other info from me!

Thanks!
-Greg
Best Answer chosen by ChickenOrBeef
Gupta.VishalGupta.Vishal
here you Go !!!! 


trigger MainTriggerAccount on Account (after update) {
  
    if(trigger.isAfter){
        if(trigger.isUpdate){
            transferContactsClass updater = new transferContactsClass();
            updater.updateContacts(Trigger.new,Trigger.oldMap);
        }
    }
}







public class TransferContactsClass{
  
      public void updateContacts(List<Account> accounts,Map<id,Account> oldaccounts) {
        
      Set<Id> accountIds = new Set<Id>();
      Contact[] contactUpdates = new Contact[0];
    
      for (Account a : accounts) {
         if (a.OwnerId != oldaccounts.get(a.Id).OwnerId) {
            accountIds.add(a.Id);
         }
      }
    
      if (!accountIds.isEmpty()) {
         for (Account act : [SELECT Id, OwnerId,
                                (SELECT Id, OwnerId, Owner.Title FROM Contacts)
                              FROM Account
                              WHERE Id in :accountIds]
             ) {
            for (Contact c : act.Contacts) {
               if (c.OwnerId != act.OwnerId && c.Owner.Title != 'BD Rep') {
                  Contact updatedContact = new Contact(Id = c.Id, OwnerId = act.OwnerId);
                  contactUpdates.add(updatedContact);
               }
            }
       
         }
         update contactUpdates;
     }
  }
}

All Answers

Gupta.VishalGupta.Vishal
HI Greg , 

You can not direclty write the trigger code in your class , you have to restucture your method and you have to send Trigger.old to the class method so that you can use it . Trigger.somthing will not work in class !!

Hope this helps !!
MagulanDuraipandianMagulanDuraipandian
From trigger:

pass trigger.OldMap

In Controller:

Get the value using Map<Id, Object_Name>

If this solves your problem, kindly mark it as the best answer.

Regards,
Magulan
http://www.infallibletechie.com
ChickenOrBeefChickenOrBeef
Hey guys,

Would you be able to make the changes for me? I'm still quite new to APEX, so I'm not sure how to go about this.


Here is the main trigger:

trigger MainTriggerAccount on Account (after update) {
   
    if(trigger.isAfter){
        if(trigger.isUpdate){
            transferContactsClass updater = new transferContactsClass();
            updater.updateContacts(Trigger.new);
        }
    }
}





And of course, here is the class:

public class TransferContactsClass{
   
      public void updateContacts(List<Account> accounts) {
         
      Set<Id> accountIds = new Set<Id>();
      Contact[] contactUpdates = new Contact[0];
     
      for (Account a : accounts) {
         if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
            accountIds.add(a.Id);
         }
      }
     
      if (!accountIds.isEmpty()) {
         for (Account act : [SELECT Id, OwnerId,
                                (SELECT Id, OwnerId, Owner.Title FROM Contacts)
                              FROM Account
                              WHERE Id in :accountIds]
             ) {
            for (Contact c : act.Contacts) {
               if (c.OwnerId != act.OwnerId && c.Owner.Title != 'BD Rep') {
                  Contact updatedContact = new Contact(Id = c.Id, OwnerId = act.OwnerId);
                  contactUpdates.add(updatedContact);
               }
            }
        
         }
         update contactUpdates;
     }
  }
}



Thanks!
-Greg
Gupta.VishalGupta.Vishal
here you Go !!!! 


trigger MainTriggerAccount on Account (after update) {
  
    if(trigger.isAfter){
        if(trigger.isUpdate){
            transferContactsClass updater = new transferContactsClass();
            updater.updateContacts(Trigger.new,Trigger.oldMap);
        }
    }
}







public class TransferContactsClass{
  
      public void updateContacts(List<Account> accounts,Map<id,Account> oldaccounts) {
        
      Set<Id> accountIds = new Set<Id>();
      Contact[] contactUpdates = new Contact[0];
    
      for (Account a : accounts) {
         if (a.OwnerId != oldaccounts.get(a.Id).OwnerId) {
            accountIds.add(a.Id);
         }
      }
    
      if (!accountIds.isEmpty()) {
         for (Account act : [SELECT Id, OwnerId,
                                (SELECT Id, OwnerId, Owner.Title FROM Contacts)
                              FROM Account
                              WHERE Id in :accountIds]
             ) {
            for (Contact c : act.Contacts) {
               if (c.OwnerId != act.OwnerId && c.Owner.Title != 'BD Rep') {
                  Contact updatedContact = new Contact(Id = c.Id, OwnerId = act.OwnerId);
                  contactUpdates.add(updatedContact);
               }
            }
       
         }
         update contactUpdates;
     }
  }
}
This was selected as the best answer
ChickenOrBeefChickenOrBeef
Thanks, Vishal! It works!