You need to sign in to do that
Don't have an account?
Zach Delacroix
Help with Trigger Handler
Hi Experts!
I'm a newbie and I've created a Trigger below with the help of awesome people here. This works fine but my friend suggested to study about the Trigger Handler so we can limit triggers in object and my code will be easier to debug. I want to learn the Best Practices on this so please help and advise how to put this with Class.
I'm not sure how to start though. basically, I thought of building a class like below.
Here's what my code does:
When Job Record is Created or Updated, find Rate Card from Account (Related List) and update the Job's Hourly Rate if the Ratecard has the same Type as the Job.
Result Should be something like below.
It would be fantastic if there are documents you can reffer me so I can start learning about this :)
Thanks in Advance!
I'm a newbie and I've created a Trigger below with the help of awesome people here. This works fine but my friend suggested to study about the Trigger Handler so we can limit triggers in object and my code will be easier to debug. I want to learn the Best Practices on this so please help and advise how to put this with Class.
trigger UpdateHourlyRate on Job__c (Before insert, Before update) { Set<Id> AccID = new Set<Id>(); Set<String> JobType = new Set<String>(); Map<String,Rate_card__c> RateCardMap = new Map<String,Rate_card__c>(); for (Job__c Job: Trigger.new){AccID.add(Job.Account__c); JobType.add(Job.Type__c);} For(Rate_Card__c RateCard : [SELECT Id, Hourly_Rate__c, Account__c, Type__c FROM Rate_card__c WHERE Account__c IN: AccID]){ RateCardMap.put(RateCard.Account__c + RateCard.Type__c, RateCard); } For (Job__c Jobs: Trigger.new ){ if(RateCardMap.containsKey(Jobs.Account__c+Jobs.Type__c)){ Jobs.Hourly_Rate__c = RateCardMap.get(Jobs.Account__c + Jobs.Type__c).Hourly_Rate__c; } }
I'm not sure how to start though. basically, I thought of building a class like below.
public class JobHourlyRateUpdate { Set<Id> AccID = new Set<Id>(); public Static void computeHourlyRate(List<Job__c> Jobs){ For(Job__c Job:Jobs){ AccID.add(Job.Account__c); } Map<String,Rate_card__c> RateCardMap = new Map<String,Rate_card__c>(); For(Rate_Card__c RateCard : [SELECT Id, Hourly_Rate__c, Account__c, Type__c FROM Rate_card__c WHERE Account__c IN: AccID]){ RateCardMap.put(RateCard.Account__c + RateCard.Type__c, RateCard); } } }
Here's what my code does:
When Job Record is Created or Updated, find Rate Card from Account (Related List) and update the Job's Hourly Rate if the Ratecard has the same Type as the Job.
Result Should be something like below.
It would be fantastic if there are documents you can reffer me so I can start learning about this :)
Thanks in Advance!
It is always a best practice to write a single trigger on object and process all operation in apex class (we called it as handler) instead of
writing logic in trigger. In handler you can specify the order of event based on different context variables. If you write many triggers in on object,
then you cannot control the order in which triggers gets executed.
For best practice, use below suggestion:
1. Pass all trigger context variables to static method (say "OperationManager")in handler class.
2. Create different static methods for different operations which you need to perform.
3. Control the order of different operation in "OperationManager" static method based on context variables.
4. Also you can create different apex classes and call them from "OperationManager" static method.
For example, below is modified code as per best practice.
Apex trigger code:
Apex class(Handler):
Refer below URL for Apex Trigger Best Practices
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices (https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices" target="_blank)
Hope this will help you.
[If it solves your problem, please mark it as solution]
Thanks,
Sunil Kumar
All Answers
You can follow the beloe link.
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
Please mark this as solution if this solves your problem, So that if anyone has this issue this post can help
It is always a best practice to write a single trigger on object and process all operation in apex class (we called it as handler) instead of
writing logic in trigger. In handler you can specify the order of event based on different context variables. If you write many triggers in on object,
then you cannot control the order in which triggers gets executed.
For best practice, use below suggestion:
1. Pass all trigger context variables to static method (say "OperationManager")in handler class.
2. Create different static methods for different operations which you need to perform.
3. Control the order of different operation in "OperationManager" static method based on context variables.
4. Also you can create different apex classes and call them from "OperationManager" static method.
For example, below is modified code as per best practice.
Apex trigger code:
Apex class(Handler):
Refer below URL for Apex Trigger Best Practices
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices (https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices" target="_blank)
Hope this will help you.
[If it solves your problem, please mark it as solution]
Thanks,
Sunil Kumar
I tried this in my test and It worked perfectly! However, I'm still having difficulty with it and don't fully understand how it really works.
I will read more about Trigger Handler and I'll make your code as my guide :) This is where I will start learning.. You're the best!
Thanks to @Mudasir as well for the Idea..
\m/