June SDG - Apex Best Practices
-
Upload
josep-vall-llovera -
Category
Software
-
view
228 -
download
1
Transcript of June SDG - Apex Best Practices
![Page 1: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/1.jpg)
Apex Best PracticesJosep Vall-llovera - CTO at Clapps
19/06/2015
@valnavjo Valnavjo in/jvn84 Valnavjo
![Page 2: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/2.jpg)
Introduction
2
![Page 3: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/3.jpg)
#1 Naming conventions
3
1. SF doesn’t provide packages to organize classes.2. Use naming conventions (English).
![Page 4: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/4.jpg)
#2 Singleton pattern
4
1. Same instance per execution request.2. Useful to avoid repeating unnecessary queries.
![Page 5: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/5.jpg)
#3 Util classes
5
1. Always use Singleton Pattern + Utils class.2. For example, Record Types.3. Do not query the same RT more than once.
![Page 6: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/6.jpg)
6
#4 Trigger Template1. Do not have more than one Trigger for the same
SObject.2. Salesforce doesn’t ensure the order of execution.3. Avoid chaos.
![Page 7: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/7.jpg)
7
#5 Bulkify1. All logic methods must be prepared to deal with
collections.
![Page 8: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/8.jpg)
#6 Group collections
8
1. Use Maps & Lists to group collections when bulkifying code.
![Page 9: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/9.jpg)
#7 Interfaces
9
1. Use interfaces when they are necessary.2. Focus on productivity.
![Page 10: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/10.jpg)
#8 Unit Tests
10
1. Unit Tests must be UNIT.2. Avoid @seeAllData = true (only for pricebooks).3. Use public class with @IsTest for data initialization.4. Just three things: prepare data, do test and assertions.
![Page 11: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/11.jpg)
#9 Use System.Limits1. Avoid governor limits.2. For example, when performing a search with SOSL.
11
![Page 12: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/12.jpg)
#10 Keep things simple
12
Logic
TriggerHandler
Utils
UI
API Helper
Trigger
Controller
Service
Unit Tests
![Page 13: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/13.jpg)
Useful links1. Apex Code Best Practices2. Context Variable3. Triggers4. My Trigger Template5. Use of the Limits Apex Methods
13
![Page 14: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/14.jpg)
Code resources
https://github.com/Valnavjo/sdgbarcelona.git
14
![Page 15: June SDG - Apex Best Practices](https://reader030.fdocuments.us/reader030/viewer/2022032506/55cb611ebb61eb5e528b466c/html5/thumbnails/15.jpg)
Thanks!
Q&A15