Salesforce Development Best Practice

There are various approaches for developing code in salesforce but there is always one way which is better than others. So here is a list of best practices that should be followed while working in Salesforce.

Apex
1. You can write the code in any format as you want but you should use the capitalization for class names. For example:

public class EmployeeDetails{}

Use Camel case for function and variable names. For example:

String empId = ‘’; Public function employeeAdd{}

You should keep in mind that the name should be descriptive for anyone who reads it so that they could understand what the class or function is actually doing.

2. Use asynchronous apex logic for bulky or long processes instead of executing the code synchronously as this may end up giving you an error of timeout.

3. Use proper exception handling wherever needed by using try and catch block so that no technical message is shown to the customers as they may not understand it. Instead, catch the error in the catch block and convert it into a user-friendly message.

4. Write the code in such a manner that it does not reach the governor limits of salesforce because if it does, it will not allow you to process your code.

5. You should never use hard-coded Ids and you should retrieve them using other queries. If you will bring the hardcoding into practice it may affect when you package your code and install it on your customer orgs. That id may change there and thus this may bring up an error.

6. Never use DML operations, asynchronous calls and queries inside the loop instead build a map or a list and then pass it after the loop.

7. Use proper validation rules on the data so that you do not include any such data that is not required or not appropriate.

Visualforce
1. Never hard code or use static values for a picklist in Visualforce pages. You should pass them in your apex class and then use it.

2. The best practice is to call the CSS files at the top and Javascript files in the end. You should follow this structure to make the page load faster. If you are writing it inline then you should write it at the top after calling the core CSS and Javascript files so that everything is in one place.

3. Use to iterate over large amount of data.

4. Add cache attribute to your tag if you want the CDN to cache your page appropriately.

5. Don’t use the Apex variables unnecessarily if they are not required, as this may reduce the speed of the page.

6. Use proper pagination to send data from Apex to Visualforce instead of all data at once as this may give you a “ViewState” error and you may not be able to display the results.

Triggers
1. It is better to streamline multiple triggers into a single one if they are working on the same object.

2. Avoid making complex logics in the trigger. Instead, make different Apex Handler classes for each trigger and write the code in it accordingly and call it in your trigger.

3. Triggers should be bulky and must have the capacity to handle large amounts of data.

4. Execute DML operations in the collection instead of processing individual operations. While retrieving the data use “where” condition with multiple conditions to get the specified data you want in one query in place of using different SOQL queries to avoid ‘too many SOQL queries’ limit issue.

5. Using a consistent naming convention for all your Triggers like “PolicyTrigger”.

Testing
1. When writing test methods ensure that you are using @isTest annotation before your class and creating all the records using the same test method instead of using already created records in the current org.

2. You should see that most of the code gets covered in testing. Although the clearing level is 75%, as the refinement of the application depends on coverage.

3. Write Pass and Fail test methods to test for different scenarios.

4. When testing use Test.startTest and Test.stopTest and the Limit class instead of hard-coding governor limits.

5. Make a base class for all the repeating records creation and call it wherever you want instead of making it in every test class to reduce the repetition of code.

6. Always use assert() method to check if the code ran as expected.