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
Ravi Kumar 259Ravi Kumar 259 

what is the best practice to write a trigger

sandeep sankhlasandeep sankhla
Hi Ravi,

Following are the best practises for triggers..

1. We should always have one trigger for one object...
2. We should make use of handler class instead of writting code inside trigger..
3. You can define different methods inhandler class based on event which you can call fro trigger..
4. Use recurrision handler to avoid the recurssion.
5. DOnt use DML inside forloop..
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

Please refer above link for more details...

Thanks,
Sandeep
TheRealistTheRealist
1.Always try to have one trigger on one object rather than having multiple triggers.
2.Try to define the dml operations outside the loop (Bulkyfying),so it would not touch the governor limit, that means at maximum Total number of   DML statements issued are 150
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm
3.Most of the times try to avoid recurrsion of the triggers (read about recurssive triggers)
4.if you do have a requirement that leads to recurssion of the triggers then define Static boolean variable within your trigger.
Let me know if you need further info about this.
mark this as best answer if this solves your problem,so that this may be helpful to someone else.