- SalesforceChaCha
- Posts
- ๐ Powerful weaponry for devs ๐บ
๐ Powerful weaponry for devs ๐บ
Wield it with care โ๏ธ
Good morning, Salesforce Nerds!
Now, this is a story all about how my life got flipped-turned upside down. โฌ
Iโd like to take a minute โ๏ธ - just sit right there.
๐ ChaCha ๐บwill tell you all about developing dynamic software.
On Salesforce, of course ๐

not too much! just the right amount!
Agenda for today includes
Powerful weaponry for Salesforce devs
Daily Principle
All the Memes
Powerful weaponry for Salesforce devs
Today we will discuss -
๐ Dynamic Apex๐บ
๐ Simple diagrams ๐บ
๐ Show me some code! ๐บ
โก๏ธ Dynamic Apex
Letโs first level-set on what we mean by โDynamicโ Apex. ๐๏ธ
Iโll let you Google Dynamic Programming Languages and sift through the official definitions on your own time. ๐
A lot of what you might find wonโt apply here. โ
From the perspective of Apex, what weโre talking about is the ability to write code that is generically typed. โ
This allows devs to write generic code that can be reused across any SObject type. ๐คฏ
Hereโs how: ๐๏ธ
Weโre all familiar with SObjectโs, right โฆ
Standard or custom objects that store our record data in Salesforce
But, did you know that in the ๐๏ธ world of Apex - SObject is also something called a superclass?
And specific SObjectโs like Account, Contact, Opportunity, That_Stupid_Custom_Object__c, etc. are all subclasses of SObject! ๐ฅ
Itโs a classic inheritance model ๐
๐ Superclass: Class from which many subclasses can be created. AKA parent class, base class.
๐ Subclass: Class derived from the superclass. It inherits the properties of the superclass and also contains attributes of its own.
๐ Simple Diagrams:
Just peep this diagram as an example: ๐
โ๏ธ The Vehicle superclass sits at the top as the most generic type. It has basic Vehicle properties like color, wheels, doors, etc.
โ๏ธ The Car subclass inherits all of the same properties from Vehicle (color, wheels, doors, etc.) and contains some specific properties like trunk, backseat, etc..
โ๏ธ The Truck subclass also inherits all of the same properties from Vehicle and contains some specific properties like Bed, Tailgate, etc..
โ๏ธ The Motorcycle class also inherits โฆ well, you get it. ๐

subclass inherit from superclass
The same concept applies to writing Apex against standard and custom objects!
That means this diagram โ๏ธ can be modified to look like this ๐๏ธ

subclass inherit from superclass
To an Salesforce engineer, this means that there is code we can write against the SObject superclass that will work across all subclasses. ๐ฅ
Just checkinโ in โฆ This is a deeeeeep rabbit hole ๐ฐ
Believe me, weโre just scratching the surface here. Imma save some of the more juicy bits for later, mkay? ๐๏ธ
๐ป๏ธ Show me some code!
So, letโs see a quick example with a super common use case:
๐ Requirement:
When an Account field is changed, cascade this to the related Contacts inside an existing Trigger handler method.
Basic stuff. Hereโs some super-simple Apex to do just that with the phone number.

This โ๏ธ code loops over the records in the trigger variable, checks if the phone number was updated. If so, add it to a List for later processing. ๐
We can imagine the same kind of code existing across other SObject types, too. Iโve seen it many times.
This works well on a small ๐ค scale, but what about enterprise systems? Or ISVโs? ๐ค
Knowing what we know now, we can leverage โDynamicโ Apex to refactor this (and other similar code) to use a more generic method like this against the SObject superclass:

This โ๏ธ code takes a Set of SObjectField tokens as a parameter. Then It loops over the records in the trigger variable. For each record, it starts looping over the passed in tokens to check if the field value changed or not with the .get() method.
Now, weโre able to refactor that first code to this:

This code โ๏ธ passes in a Set instance containing the Account.Phone token to the generic getModifiedRecords() method and stores the result in a List<Account>
Daily Principle
"So many of our dreams at first seem impossible, then they seem improbable, and then when we summon the will, they soon become inevitable."
and now....Your Daily Memes

jrโs gonna jr

gotta be careful here

What did you think about today's newsletter? |