- SalesforceChaCha
- Posts
- 💃 Upcoming Apex Features 🕺
💃 Upcoming Apex Features 🕺
Spring '24 release preview
Good morning, Salesforce Nerds! Are you keeping up with the new features coming to Apex soon?
If not, don’t sweat it. The ChaCha’s got you!
I saw some pretty slick stuff coming down the pipeline for Apex so I thought I’d throw my favs into a list. 👍️
Seems the Apex team has been busy cranking out useful features!

Gotta love these new features!
TABLE OF CONTENTS
Upcoming Apex Features
Null Coalescing Operator
This one’s super useful.
How many times have you needed to perform a null check in code? 🤔
Probably a lot. It’s a necessary thing.
These checks can be scattered throughout your codebase and become rather verbose in spots. 🙄
For example, here’s a few options for doing this:
// Assign the value of myVar or 100 to newVar
//* Option 1 *//
Integer newVar = (myVar == null) ? 100 : myVar;
//* Option 2 *//
Integer newVar;
if(myVar == null) {
newVar = 100;
} else {
newVar = myVar;
}
In these two examples, we check the value of myVar and assign it’s value to newVar only if it’s not null. Otherwise we assign the value of 100 to newVar.
Super basic, but just think about code like this peppered throughout your org. Not super great.
Instead of this code 👆️, devs will be able to use the new Null Coalescing Operator to perform the same check!
We’ll be able to refactor the code above into this:
// Assign the value of myVar or 100 to newVar
Integer newVar = myVar ?? 100;
The ?? operator returns the left-hand argument if the left-hand argument isn’t null. Otherwise, it returns the right-hand argument.
Looking forward to this one! 💯
Randomly Generated UUID
Have you ever needed to generate a UUID in Apex?
I have. It took some Googling for a solution, but I eventually found one.
The Apex team has gone to work to deliver us a new feature that will make this MUCH simpler.
Check out the old way vs the new way:
// Old way of creating UUID
Blob b = Crypto.GenerateAESKey(128);
String h = EncodingUtil.ConvertTohex(b);
String uuidStr = h.SubString(0,8) + '-' +
h.SubString(8,12) + '-' +
h.SubString(12,16) + '-' +
h.SubString(16,20) + '-' +
h.substring(20);
// New way
String uuidStr = UUID.randomUUID().toString();
Boom! 💥
I love it replacing multiple lines of code with a single line!
Compress/Extract Zip Files
If you’re in a position where you deal with a lot of large files then this may be a life saver for you! 🛟
There will soon be a new namespace added called Compression.
This namespace will contain functionality that allows you to easily generate and extract compressed zip files! 🔥
Yep, you read that right.
Need to send a large attachment to a customer via email? Soon, you’ll be able to zip it up directly in Apex!
Maybe you want to compress older Content Documents to save file storage? You can do something completely on platform after this release!
I’m gonna spare the code here, but have a look if you’re interested! 👀
Evaluate Dynamic Formulas
This is an interesting one!
This is going to allow devs to evaluate user-defined formulas against both Apex object and SObjects.
Meaning, we easily calculate formula expressions without having to create a traditional formula field. 🤯
We just write the Apex for it!
Let me show you an example straight out of the release notes:
global class MotorYacht {
global Integer lengthInYards;
global Integer numOfGuestCabins;
global String name;
global Account owner;
}
MotorYacht aBoat = new MotorYacht();
aBoat.lengthInYards = 52;
aBoat.numOfGuestCabins = 4;
aBoat.name = 'RV Foo';
FormulaEval.FormulaInstance isItSuper = FormulaEval.FormulaBuilder.builder()
.withReturnType(FormulaEval.FormulaReturnType.STRING)
.withType(MotorYacht.class)
.withFormula('IF(lengthInYards < 100, "Not Super", "Super")')
.build();
isItSuper.evaluate(aBoat); //=> "Not Super"
To create an instance of the formula, call the static method builder() in the FormulaBuilder class by specifying the formula text, return type, and context object (the MotorYacht Apex object in this case).
To validate the formula instance, call the build() method. If the validation fails, the build() method triggers the FormulaValidationException exception.
SOUL FOOD
Daily Principle
"You only live once, but if you do it right, once is enough."
and now....Your Daily Memes

Sigh … not again

Apex compiler when the null coalescing operator is executed

it’s so much cleaner
What did you think about today's newsletter? |