Salesforce Developer Best Practices Checklist.
1. Explore Declarative Options
- Before creating an apex class or a Lightning component please check if the functionality can be achieved declaratively (Flows, Validation Rules, Dynamic Forms etc…)
2. Use Trigger Framework
- There can only be one unmanaged trigger per object.
- The trigger contains no logic.
- There is no work in the trigger-handler.
- Work is being done in helper classes.
- Handler is context-dependent.
- Recursion is prevented by the trigger/trigger handler.
- In bypass framework, custom settings are used.
- Before inserting/updating context, make any necessary changes to the current record.
3. Code is Bulkified
- There are no SOQL queries in loops
- All SOQL queries are selective
- Code includes null checks
- There are no DML statements in loops
4. Error Handling
- Use try/catch when possible
- Log erros in custom object
- Notify Admins depending on the severity of the errors
5. Use Asynchronous Apex if possible
- Asynchronous Apex (Batch Apex, Queueable Apex, Scheduled Apex, and Future methods) has higher governor limits.
6. Avoid Hardcoding
- Use Custom Metadata, Labels or Custom Settings to avoid hardcoding.
7. Add Appropriate Code Comments
- Adding appropriate code comments help increasing maintainability of the code.
8. Create Appropriate Test Classes
- Although Salesforce enforces 75% code coverage, aim for at least 90% code coverage
- Make sure Tests cover positive scenario
- Make sure tests cover positive as well as negative scenarios
- Make sure tests include bulk scenarios
- Use System.Assert to validate that code performs expected functionality
- Make sure test classes do not use SeeAllData
- Use startTest() and stopTest()
- Leverage mock testing framework to test web services
- Leverage @testSetup annotationfor creating data that can be re-used in all test methods.
9. Lightning Web Components Best Practices
- Before you start building your own custom Lightning components you should familiarize yourself with the library of base components. Leveraging these base components can significantly speed up your development time
- Data is lazy loaded when possible and does not preload
- Calls to server are limited (pass info between components when possible)
- Queries only contain columns in SELECT that are used/needed
- Data caching is used when possible
- Code uses static schema instead of dynamic schema when possible
- Code DOES NOT use pub/sub for parent-child or child-parent communication
- Code does not make use of
window.location
- Each LWC has a Jest test that tests the component’s behavior in isolation
- Use Cacheable Apex Methods
- Use Conditional Rendering
- Lists should be created using either
for:each
oriterator
- Leveraging events
- Whenever possible, remove dependencies on unneeded Third-Party libraries
- Whenever possible, use the (sprite-based) SLDS icons (using
<lightning-icon>
and<lightning-button-icon>
) instead of custom icons. - Minimize the number of times your component is being re-rendered
10. Performance Best Practices
- Integrations are asynchronous if there is not a real time requirement
- Multiple requests are combined into single composite request when possible
- Queries were run through Query Planning Tool
- Use caching where ever possible
Recommended Articles