You need to sign in to do that
Don't have an account?
Michael 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
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
Please find below trigger and class code:
Trigger:
Class
Thanks,
Gaurav
Skype: gaurav62990
All Answers
Please find below trigger and class code:
Trigger:
Class
Thanks,
Gaurav
Skype: gaurav62990
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
Please update with
Thanks,
Gaurav
Thank you....it is working now.
Michael
For the trigger, you may have to specify both the class and method, not just the class.
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:
you'll write:
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.
Anyway. I hope this helps. And i hope this is correct. It works in my org.