Best Practices plays an Important role in our Salesforce development. How? When you follow the best practices then you have very little chance of getting errors in your development work.
Best Practices in Salesforce
1.Bulky Your Code
Your code always works for a list of records not for a single record. So that your code will run in a proper manner. It will not be going to break when you add lots of records to your org and it will improve your code performance.
2.Avoid DML/SOQL Query in loop
If you use DML or SOQL query in your Loop statement then loop will perform DML or SOQL Query no of time and it will be going to hit your heap limit. So that you have to perform SOQL query and DML once in your code. So, it will help to Improve your code performance.
3.Avoid Hard coded ids
Your code must be dynamic. It always takes a dynamic value. So that if anyone changes that value then your code is also not going to break. If you deploy code in another org then in that org your id will change it will lead to break your code. So always avoid hardcoded ids in your code.
4. Use one trigger for one object
If there are multiple triggers on a single object then we cannot define the order of execution of those triggers. Which one is going to execute first we cannot define. So that always creates a single trigger for a single object and helps you in the Salesforce Development.
5. Test Classes in Salesforce
- Always Prefer of creating multiple method inside your test class, so it will help to maintain your test class.
- Avoid use of seealldata = true in your test class methods.
- Asserts statements are very important so always use assert statement to check your result.
- Use of a @testSetup method inside your test class will help to reduce execution time and increase maintainability.
- Always Use test.startTest() and test.stopTest() for calling of your class or execution of your test method.
- Prefer target 85% code coverage per class
6. Visualforce
- Always try to create a simple and light page for good performance and usage.
- Try to use less command statement in your code, because it will affect your code performance.
- Try to use {!$Page.PageName} or {! URLFOR ($Page.PageName)} instead of (value=”/apex/PageName”) because it will hardcode access to Visualforce Pages as it will break when CRSF is enabled.
- Reduce the framework usage to keep the page light and in good performance (RAM footprint, network impact, CPU consumption…)
7. Profiles
- Always try to keep fewer profiles in your org. If one profile has few more permission rather than another existing then use permission set.
- Try to remove unused custom profiles from your org.
- Profiles should not be API enabled, except when really necessary, for security reasons. If necessity of making profile API enabled for security reasons, then only you can make profile API enabled profile. Otherwise try to make profile not be API enabled.
8. Validation Rules
- Do not use hard coded IDs in your validation rule.
- Try to bypass the system admin profile in that validation rule.
- Try to create less validation rules to increase your performance and usage.
- Try to define the error message at the field location instead of at the top of the page.
- Remove inactive Validation Rules from your org.
9. Workflow rules and actions
- Do not use hard coded IDs in your workflow rule.
- Don’t create multiple workflows with the same logic.
- Remove excessive workflow rules which are not in use.
- Remove that workflow rules also which don’t have any actions
- Try to keep the fewer in quantity of workflow rule to manage performance of your org and usage.
10. Naming conventions
- Avoid using underscores while giving any name to any functionality.
- Apex Class name should be in UpperCamelCase
- Ex – MyFirstApexClass
- Variables and methods in apex class always use camelCase with the first letter in lowercase.
- Ex – Variable – myFirstVariable
- Method – myFirstMethod
- Test class name should be had ‘Test’ at the end of the Class name.
- Ex – MyFirstApexClassTest
- Trigger name should be like this – MyFirstTrigger
- Batch class name should be like this -MyFirstBatchClass
I hope this information will be helpful for you.
Thank you.
Leave a Reply