Monday, 2 December 2013

salesforce best practices for developers

Coding in apex requires that you follow all the best practices, as you may hit the governers limit imposed by salesforce.
Let us discuss some of the best practices that you should follow in order to avoid production failures and exceptions
1. Avoid writing select queries within for loop.
   This is something which you should avoid in all cases as this could very easily give "101 soql error"
   You can fire only 100 queries in one instance of apex code run, if more than 100 queries get executed you get the  above exception. Hence write your logic in such a way that you do not use select query within for loop
2. Use collections to store you data in apex, use map list set appropriately.
3.Bulkify your trigger, most often developers write triggers considering only one record would fire the trigger. But, in reality your trigger could be fired by many records at a time. Make sure you write a logic in such a way that it runs considering many records would fire the trigger at a time
4. Clear collections that are no longer required in apex code.Use transient key word where ever possible. View state error can be avoided by doing this.
5. Try to use limit clause in you select query, a select query can return only 50k records.
6. sosl query can return only up to 2000 records hence before using sosl you have to be 100% sure that the returned records will be less in number.
7. No dmls within for loop.
  Avoid updating,inserting or any other dmlon records in for loops. Instead run your dmls on collections. You can issue only 150 dml statements.
8. Write your code logic with minimum statements. Try to reduce number of lines of apex code if possible.
  There's limit for number of lines too.
9. Do not write your logic in trigger, write it in a class method and call that method in trigger.
10. Use access modifiers appropriately.
11. try to re-use code that has been already developed
12. Put your code in try and catch blocks appropriately and catch the exceptions
13. Follow this: One object, one trigger.
14. Follow naming conventions throughout your org, this keeps your codes organised and easily understandable.
15. Develop generic components for functionality that are used in many vf pages.
16. For Test coverage, make sure all the triggers in your org are covered with at least 1%.
    Total coverage needs to be > 75%, classes can have 0% but trigger should have minimum of     1%

No comments:

Post a Comment