๐Ÿ’ƒ 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! ๐Ÿ•บ

salesforce

Job Board | ๐Ÿ’ƒ Click Here ๐Ÿ•บ

Socials | ๐Ÿ’ƒ Linkedin | Instagram | Twitter!๐Ÿ•บ

โšก๏ธ 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."

Christopher Reeve

and now....Your Daily Memes

jrโ€™s gonna jr

gotta be careful here

 

What did you think about today's newsletter?

Login or Subscribe to participate in polls.