You need to sign in to do that
Don't have an account?
SFDC Guest
Convert apex trigger into helper class
Hi All,
I am using below trigger to update child records. Can someone please convert logic into helper class and call it from trigger. Thanks in advance.
I am using below trigger to update child records. Can someone please convert logic into helper class and call it from trigger. Thanks in advance.
trigger ContactUpdate on Account (after update) { Map < Id, Account > mapAccount = new Map < Id, Account >(); List<Contact> listContact = new List<Contact>(); for(Account acct : trigger.new) mapAccount.put(acct.Id, acct); listContact = [ SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ]; if ( listContact.size() > 0 ) { for ( Contact con : listContact ) { con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet; con.MailingCity = mapAccount.get(con.AccountId).BillingCity; } update listContact; } }
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Trigger:
Handler:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
All Answers
if you want to create a helper class for this. You have to create a handler class too. First create a handler class and call that handler class in this trigger with events and all as it it. Then put your logic in a helper class. Have all the methods and logic in helper class and call the helper class in handler class. Like if method in helper is static. Go with
Class name.method name();
Please let me know if you have doubt
Thanks
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Trigger:
Handler:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Trigger:
All that aside ... maybe something like this?
I know some people like to make their handler more generic and just call an "AfterUpdate" handler method. I prefer to see what is being done when I look at the trigger itself. Also, best practice would be to only update the contacts that need to be updated -- the ones whose street or city differs from the account street or city.
(I haven't compiled or tested this, so there could be typo's.)
Please try the below code,
Handler Class : -
Trigger:-
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com