1. Thou Shalt Keep thy code dumb straightforward (KISS guideline)
- In programming advancement, applying the KISS guideline implies your code is kept perfect and succinct.
- There ought to be as little rationale in your code as could really be expected. Complex if-else articulations? Normally they can lessen to basic boolean rationale. Reusing code in a few spots? Compose a solitary technique and decrease the messiness in your code.
- Basic code is simpler to keep up. Continuously utilize the KISS standard and keep your Apex dumb basic.
2. Thou shalt not put inquiries in circles
- One way Salesforce upholds that its occupant's applications or your applications should run in a performant way. One way Salesforce does this is by implementing lead representative cutoff points.
- Quite possibly the toughest lead representative cutoff points is a SOQL inquiry cutoff of 100 questions in exchange. In the event that you at any point inquire inside a circle, you will run into this limit. Henceforth, thou shalt not put inquiries in for circles.
- Think about the accompanying trigger, which could run in clusters of 200 records. At the point when the 101st question is executed, Salesforce tosses an uncatchable LimitException. Your trigger fizzles and any work your trigger has performed is moved back.
- Terrible illustration of a SOQL inquiry inside a for circle
- SOQL questions are one of the numerous Apex devices that have lead representative cutoff points. For more data on lead representative cutoff points, see Understanding Execution Governors and Limits.
3. Thou shalt use maps for inquiries
- As exhibited in the model above, using maps for inquiries is a major procedure and stunt to use to make SOQL questions, DML exchanges, and Apex triggers more productive.
- At times, you'll be refactoring some code and go over an inquiry as a List. Fortunately, there's a stunt to getting a Map from a rundown, without a For Loop. Look at this:
Map<Id, sObject> myAwesomeMap = new Map<Id,sObject>(List of sObjects or SOQL Query);
The code above gives you a guide, without emphasis from a prior rundown of sObjects!
4. Thou shalt use connections to decrease inquiries
- Another vital procedure in making SOQL inquiries more proficient is to use connections. In the case of using connections to save SOQL outside a for circle, we used the kid's Contacts relationship of the standard Account object.
- SOQL likewise enables us to inquire for parent record data, for example, in this model. For more data on using connections in SOQL, read and bookmark Salesforce's article on Relationship Queries
Don't forget to check out: How to Convert sforce.apex.execute to Lightning | Salesforce Developer Guide
5. Thou shalt not put DML in circles
- Like the second edict, thou shalt not put DML in circles all things considered. As a speedy recap, DML implies any addition, update, erase, undelete, consolidate, convertLead exchange.
- Not exclusively is DML in a circle ineffectively performant, however, you will run into lead representative cutoff points also. To evade DML in for circles, as a rule, ensure you're following up on various records.
- The accompanying model shows how a rundown is utilized to monitor accounts that should be refreshed. When the circle is done, a solitary DML exchange is performed to refresh the rundown of records.
6. Thou shalt just utilize one trigger for every article
- What number of Account triggers does your organization as of now have? 1? 5? 10? More? As a rule, different triggers across a similar article extraordinarily increment the intricacy of your Salesforce application. Moreover, one trigger isn't ensured to fire before another trigger.
- By merging any triggers, your application is less inclined to slip-ups and imperfections, and business measures are a lot simpler to keep up.
7. Thou shalt keep rationale outside of triggers
- Triggers should just contain a modest bunch of lines of code, utilized for calling strategies. The actual trigger should just be utilized for overseeing execution requests, and representative all else to a different Apex class like a trigger overseer.
- For an illustration of a trigger controller design, look at Force.com MVP Kevin O'Hara's SFDC Trigger Framework. Additionally, when you're dealing with the execution request in your trigger, remember the request for execution for other Salesforce segments.
8. Thou shalt have an upbeat harmony among snaps and code
- Salesforce gives a huge measure of definitive usefulness with the work process, stream, approval rules, equations, and the sky's the limit from there.
- Realizing when to utilize those definitive highlights instead of hopping straight into an Apex or Visualforce arrangement isolates the incredible engineers from the remainder of the pack.
- This is particularly significant as the following not many deliveries are bringing some astonishing revelatory usefulness, including Process Builder. Cycle Builder has the ability to decisively fabricate basic triggers – taking out the need to code them!
9. Thou shalt covet thy code
- You should focus on 100% code inclusion. Despite the fact that Salesforce commands 75% code inclusion across your peak classes when you convey to creation, you ought to consistently focus on 100%.
- This isn't to say you can't go home until your group covers that last 3%, yet the objective being as high a code inclusion as is reasonable. Recollect that unit tests are for Salesforce's advantage, however, your own – building up true serenity that your application is working true to form.
10. Thou shalt compose significant unit tests
- Despite the fact that your code is 100% covered, it doesn't really mean nor attest that your code is working appropriately. To confirm your code is working and no bogus positives are returned by the unit test, use System.assert proclamations generously.
- All things considered, a unit test isn't a test without declarations. The following are a few techniques Salesforce accommodates stating unit tests:
- System.assert(A == B,'A doesn't rise to B');
- System.assertEquals(Expected,Actual,'Actual doesn't rise to Expected');
- System.assertNotEquals(Expected,Actual,'Actual rises to Expected, however ought not');
- For extra focus, use that last boundary to leave yourself significant, elucidating articulations concerning why your test bombs when the normal and real qualities don't coordinate.
11. Thou shalt compose unit tests prior to creating
- Realizing that you should compose unit tests to convey your code, why not work out your unit tests early? Working out unit tests gives extra thinking ahead into what the application does, how it handles various conditions, and how the application will deal with different conditions – particularly special case cases.
Check out another amazing blog by Mohit here: Getting Started With Salesforce Einstein Discovery
12. Thou shalt test all conditions
- When your tests are composed, your code is 100% covered, you're prepared to convey, correct? Not really. Have you tried every one of the conditions with those tests? Check for nulls, limit conditions, declare that your special cases were tossed or dealt with appropriately? A decent test class will test every one of these conditions and the sky's the limit from there, and give further certainty that your application is filling in as planned.
13. Thou shalt never utilize faker code inclusion
- One path into getting around the 75% code inclusion highlight is to create classes with pointless lines of code, for example, i++. These spurious code inclusion classes accomplish more mischief than anything. Faker code inclusion gives an incorrect feeling that all is well with the world around the application, and possibly permits extra deformities to sneak through the breaks when testing the application. Sham code inclusion makes Astro cry, and you don't need Astro to cry, isn't that right?
14. Thou shalt never test with existing information
One of my number one inquiries across the designer discussions goes something as per this:
- "My unit test has turned out great underway for quite a long time. Out of nowhere, it's tossing an exemption and now I can't convey my code, if it's not too much trouble, help"
- This is regularly a consequence of depending on existing information in your Salesforce case. Since the test information depends on existing information, it will flop once the ward record(s) are erased from the System. Since the test falls flat, so do any resulting arrangements.
- The solution for the present circumstance is to consistently make mock information and never utilize the @isTest(SeeAllData=true) comment in unit tests nor depend on existing information.
15. Thou shalt not present additional rationale for tests
- It used to be that designers expected to utilize Test.isRunningTest or another rationale to abstain from making callouts and dealing with exemptions in their unit tests.
- With the coming of the HttpMock and WebserviceMock interfaces, there is little requirement for the Test.isRunningTest() strategy. In the event that there is as yet a requirement for infusing extra code for unit tests, at that point consider taking a gander at the @TestVisible explanation all things being equal.