We are experiencing performance issues on our org and over the years the size of the org (data + bus logic components like workflows, triggers, fields etc..) have grown rapidly and exponentially.
We are planning to review the trigger code manually, could you please suggest:
- If there are any trigger practices / or link to the triggers best practices document
- What are the TOP 5 major coding components that we need to look for while trying to improve the trigger code.
Attribution to: user5352
Possible Suggestion/Solution #1
You may look into following links for some of the best practices https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bestpract.htm http://wiki.developerforce.com/page/Apex_Code_Best_Practices
Attribution to: codebandit
Possible Suggestion/Solution #2
There is a lot to talk about and depending on your case there might be different solutions how to improve and refactor.
What I'd suggest first is to map out all triggers and workflows against each object and identify the dependencies. Then start looking at merging code and moving out code from triggers to classes and static methods. For each object I would create 1 trigger only and each piece of logic would be in a separate method which will be called from the trigger. That way you get very good visibility of the order of execution of things in your trigger.
Things to look out for and must do's:
1) SOQL queries in for loops
2) DML statements in for loops
3) Recursive triggers (calling each other X times) - you need to make sure each trigger is only called once and implement some mechanism to prevent from recursion
4) Use maps and avoid nested for/do-while loops
5) Comments, lots of comments
I am sure there is a lot more that I've missed and can be found in the documentation for apex triggers best practices and someone might have a different approach when it comes to complex trigger logic. Hope this helps you get started.
Attribution to: Boris Bachovski
Possible Suggestion/Solution #3
Over the years of working with Salesforce.com my team and I used a different trigger pattern and we found that each project requires own trigger template thus it's hard to suggest something concrete to you, but usually we use the following link during our work on design a trigger template for new project and mix them:
http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers
http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers
http://www.embracingthecloud.com/2010/07/08/ASimpleTriggerTemplateForSalesforce.aspx
http://sfdcwizard.com/2013/05/triggerx-light-forcecom-trigger-pattern/
as well as links which has been provided by @codebandit
Attribution to: Pavel Slepiankou
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32019