- Curated list of Apex Best Practices from TrialheadDX 2017
- CREDITS : Apex 10 Commandments - https://www.youtube.com/watch?v=yqJZDmW1yLI
- Queries in For Loops can break governer limits
- Total Query limit per execution context is 100.
- Take SOQL queries out of For-Loop. Here in this case, we are using relationship queries.
- Using DML Operations inside for loops can break governer limits.
- DML Operation Limit per execution context is 150.
- Add the instance variable to a list and then perform DML operation outside of For-loop
- This is because, if we have multiple triggers per Object, salesforce does not guarentee the order of execution.
- As best practice, always create an Handler Classes
- By using Map, we can always retrieve a specific record using ID.
-
Parent-child and Child-Parent Relationships in Salesforce are useful to reduce number of Queries, so that the risk of breaking governer limits are reduced
-
When we go from Child-Parent, we can go upto 5 levels up in the hierarchy.
-
When we go from Parent-Child, it is limited to 1 level down.
-
Atleast 3 scenarios should be tested:
- Positive Test Case
- Negative Test Case
- User/Profile/Role based Case
-
Example of a Positive Test Case
- Example of a Negative Test Case
- We are forcing an error and we are checking the error which was thrown was the error which we are expecting.
- Example of User/Role/Profile based Test Case
- Use System.runAs() method for this type of test cases.
- Every test needs to use assertions
- Assert(A==B,"Reason why Assert Failed")
- AssertEquals(A==B, "Reason why Assert Failed")
- AssertNotEquals(A==B, "Reason why Assert Failed")
- Unit Test - Test Driven Development (TDD)