You need to sign in to do that
Don't have an account?
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!


Let me know if you need any other info from me!
Thanks!
-Greg
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!
Let me know if you need any other info from me!
Thanks!
-Greg
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
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 !!
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
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
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;
}
}
}