Best Practices with
Apex in 2022
LOGO
Lead Developer Advocate
@msrivastav13
Mohith Shrivastava
LOGO
Lead Developer Advocate
@codefriar
Kevin Poorman
Automates complex business processes
Multi-tenant aware, easy to test, versioned
Apex lets us extends platform capabilities
Most sought out skill if you are looking to be a
Salesforce Developer
What’s special about Apex?
100s
of
billions
Yearly
Transactions
We have divided best practices of Apex
into following categories
❏ Security
❏ Performance
❏ Designing For Scale
❏ Reusability
❏ Maintainability
Building Enterprise Apps with Apex
https://sfdc.co/apex-recipes
Apex Recipes
Scan the above QR Code to
navigate to apex-recipes git repo
Security
Improve App Security by Enforcing
CRUD/FLS
// check for create permission (CRUD Example)
CanTheUser.create(new Account());
CanTheUser.read(listcontacts);
// check for FLS permission
CanTheUser.flsAccessible(‘Opportunity’, ‘Amount’);
CanTheUser.flsUpdatable(‘Opportunity’, ‘Amount’);
// check for FLS permission when querying
new Safely.doQuery(query string);
Scan the above QR Code to
navigate to Security Recipes
Strengthen your Data Security
Comply with privacy policies and regulatory
requirements
● encrypting data at rest - Use Shield Platform
encryption
● encrypt data transmitted over network - Encryption
recipes
// An Example from Encryption Recipes
String toHash = ‘Test Data’;
Blob dataToHash = Blob.valueof(toHash);
Blob hash = EncryptionRecipes.generateSHA512HashRecipe(dataToHash);
EncryptionRecipes.checkSHA512HashRecipe(hash, dataToHash);
Scan the above QR Code to
navigate to Encryption
Recipes
Integration Best Practices in Apex
● Use Named Credential
○ Avoid storing username and passwords or API
keys if possible
● Understand limits of Apex
○ Apex might not be ideal for every type of
integration
Scan the above QR Code to
navigate to Integration
Apex Recipes
Performance
Improve speed using Platform Cache
Platform cache provides memory layer to store
session and org data for later reuse.
Some use case for platform cache below
● static data
● reused throughout the session/user requests
● expensive to compute or retrieve
// An Example from Platform cache recipe
// Store in org cache
PlatformCacheRecipes.storeValueInOrgCache(‘Account’, ‘Test Account
static data’);
// Store from org cache
PlatformCacheRecipes.getDefaultPartition(PlatformCacheRecipes.PartitionT
ype.ORG).get(‘Account’);
Scan the above QR Code
to navigate to Platform
Cache Recipes
Design for Scale
Code Bulkification & Optimization
● No DML or SOQL inside FOR loops
● Understand Apex governor limits
● Write optimized queries
● Design Code modules that can process collections
of records in a single transaction
Scan the above QR Code
to navigate to Collection
Recipes
Solve for Large Data Volumes(LDV)
Process large chunks of data using Queueables.
● You can find an example of how to process large
dataset in serial using queueables in apex recipes
● Design your queuables to be fail proof using
Transaction Finalizers
Honorable mention to functions. Functions are
great way to handle LDV use cases
Scan the above QR Code
to navigate to LDV
Recipes
Reusability
Reusability
● Adopt a trigger framework when writing triggers
with one trigger per object
● Apex-Recipes provides an opinionated
framework
■ Uses CMDT to control the order of execution
■ A bypass flag to switch off the trigger when
needed
Scan the above QR Code
to navigate to Trigger
Recipes
Maintainability
Coding Conventions and Standards
● Define and follow a uniform naming conventions
● Avoid hardcoding IDs
● Error Logging Frameworks
○ Fire platform events upon errors
● Use PMD to Scan your apex
● Implement CI & CD for the project to discover
breaking changes
Scan the above QR Code to
navigate to Success Cloud Coding
Conventions Trailhead Module
Write meaningful tests
● Use test data factories to create and manage
reusable data for testing needs
● Use Mocks for testing API callouts
● Use Stub provider to build mocking frameworks
for unit testing
● Write meaningful negative tests
Scan the above QR Code
to navigate to Testing
Recipes
RAD Women needs your help!
radwomen.org/be-a-part-
of-radwomen/
@radwomencode
Thank You

Best Practices with Apex in 2022.pdf

  • 1.
  • 2.
  • 3.
  • 5.
    Automates complex businessprocesses Multi-tenant aware, easy to test, versioned Apex lets us extends platform capabilities Most sought out skill if you are looking to be a Salesforce Developer What’s special about Apex? 100s of billions Yearly Transactions
  • 6.
    We have dividedbest practices of Apex into following categories ❏ Security ❏ Performance ❏ Designing For Scale ❏ Reusability ❏ Maintainability Building Enterprise Apps with Apex
  • 7.
    https://sfdc.co/apex-recipes Apex Recipes Scan theabove QR Code to navigate to apex-recipes git repo
  • 8.
  • 9.
    Improve App Securityby Enforcing CRUD/FLS // check for create permission (CRUD Example) CanTheUser.create(new Account()); CanTheUser.read(listcontacts); // check for FLS permission CanTheUser.flsAccessible(‘Opportunity’, ‘Amount’); CanTheUser.flsUpdatable(‘Opportunity’, ‘Amount’); // check for FLS permission when querying new Safely.doQuery(query string); Scan the above QR Code to navigate to Security Recipes
  • 10.
    Strengthen your DataSecurity Comply with privacy policies and regulatory requirements ● encrypting data at rest - Use Shield Platform encryption ● encrypt data transmitted over network - Encryption recipes // An Example from Encryption Recipes String toHash = ‘Test Data’; Blob dataToHash = Blob.valueof(toHash); Blob hash = EncryptionRecipes.generateSHA512HashRecipe(dataToHash); EncryptionRecipes.checkSHA512HashRecipe(hash, dataToHash); Scan the above QR Code to navigate to Encryption Recipes
  • 11.
    Integration Best Practicesin Apex ● Use Named Credential ○ Avoid storing username and passwords or API keys if possible ● Understand limits of Apex ○ Apex might not be ideal for every type of integration Scan the above QR Code to navigate to Integration Apex Recipes
  • 12.
  • 13.
    Improve speed usingPlatform Cache Platform cache provides memory layer to store session and org data for later reuse. Some use case for platform cache below ● static data ● reused throughout the session/user requests ● expensive to compute or retrieve // An Example from Platform cache recipe // Store in org cache PlatformCacheRecipes.storeValueInOrgCache(‘Account’, ‘Test Account static data’); // Store from org cache PlatformCacheRecipes.getDefaultPartition(PlatformCacheRecipes.PartitionT ype.ORG).get(‘Account’); Scan the above QR Code to navigate to Platform Cache Recipes
  • 14.
  • 15.
    Code Bulkification &Optimization ● No DML or SOQL inside FOR loops ● Understand Apex governor limits ● Write optimized queries ● Design Code modules that can process collections of records in a single transaction Scan the above QR Code to navigate to Collection Recipes
  • 16.
    Solve for LargeData Volumes(LDV) Process large chunks of data using Queueables. ● You can find an example of how to process large dataset in serial using queueables in apex recipes ● Design your queuables to be fail proof using Transaction Finalizers Honorable mention to functions. Functions are great way to handle LDV use cases Scan the above QR Code to navigate to LDV Recipes
  • 17.
  • 18.
    Reusability ● Adopt atrigger framework when writing triggers with one trigger per object ● Apex-Recipes provides an opinionated framework ■ Uses CMDT to control the order of execution ■ A bypass flag to switch off the trigger when needed Scan the above QR Code to navigate to Trigger Recipes
  • 19.
  • 20.
    Coding Conventions andStandards ● Define and follow a uniform naming conventions ● Avoid hardcoding IDs ● Error Logging Frameworks ○ Fire platform events upon errors ● Use PMD to Scan your apex ● Implement CI & CD for the project to discover breaking changes Scan the above QR Code to navigate to Success Cloud Coding Conventions Trailhead Module
  • 21.
    Write meaningful tests ●Use test data factories to create and manage reusable data for testing needs ● Use Mocks for testing API callouts ● Use Stub provider to build mocking frameworks for unit testing ● Write meaningful negative tests Scan the above QR Code to navigate to Testing Recipes
  • 22.
    RAD Women needsyour help! radwomen.org/be-a-part- of-radwomen/ @radwomencode
  • 23.