Salesforce | Best Practices

| By Webner

Best Practices for Salesforce

For Apex Classes:

1. Use Asynchronous Apex methods like @future methods, batch processing, scheduler that does not need to be executed synchronously.
2. Asynchronous Apex should be “bulkified”. It means able to handle collections of records.
3. Apex code must have appropriate exception handling.
4. When querying large data sets use SOQL “for” loop.
5. Use SOSL over SOQL where possible because it is much faster than SQL.
6. Avoid hitting governor exceptions in apex methods.
7. No SOQL or SOSL queries inside loops. If you need to query, query once, retrieve all the necessary data in a single query, then iterate over the results.
8. No DML statements inside loops.
9. No Asynchronous methods inside loops.
10. Do not use hardcoded IDs.

For Apex Triggers:

1. There should only be one trigger for each object because you can’t manage the order of execution of multiple triggers on single object.
2. Avoid complex logic in triggers. For testing and reuse, you should create Apex classes which contain the actual execution logic.
3. Methods should be bulkified.
4. Triggers should be “bulkified” and be able to process up to 200 records for each call.
Execute DML statements using collections instead of individual records per DML statement.
For Example:
List acclist; // Collection of records
Update acclist; //DML Operation.

5. Use Collections in SOQL “WHERE” clauses to retrieve all records back in single query.
For Example: Set Ids;
SOQL=[Select Id,Name From Account WHERE Id IN :Ids];
Use a consistent naming convention including the object name (e.g., AccountTrigger)

For Visualforce Page:

1. Do not hardcode picklists in Visualforce pages; include them in the controller instead.
2. Javascript and CSS should be included as Static Resources allowing the browser to cache them.
3. Refer all CSS on the top of the page and the script tags at the bottom of the page. This helps in improving the performance of the pages.
4. Mark controller variables as “transient” if they are not needed between server calls. This will make your page load faster as it reduces the size of the View State :
View State Size: 135kb.
5. Use to iterate over large collections.
6. Allow cache the icon graphics and images frequently used for fast loading of vf page.
7. Do not use soql queries in getters and setters. This would affect the performance of the page.

For Testing Method:

1. Use a consistent naming convention including “Test” and the name of the class being tested (e.g., Test_AccountTrigger).
2. Test classes should be @isTest annotated.
3. Test methods should create all data needed for the method and not rely on data currently in the Org.
4. Use System.assert to check that code behaves as expected.
5. Test triggers to process 200 records, make sure your code is “bulkified” for 200 records.
6. When testing for governor limits, use Test.startTest and Test.stopTest.
7. Use System.runAs() to execute code as a specific user to test for sharing rules.

Leave a Reply

Your email address will not be published. Required fields are marked *