Spring'24 Updates in Salesforce for Developers

This article is prepared by our Salesforce Developer Aliaksandr Sheuka.

Apex Enhancements

  • Null Coalescing Operator (??)

The ?? operator returns the left-hand argument if the left-hand argument isn’t null. Otherwise, it returns the right-hand argument. Similar to the safe navigation operator (?.), the null coalescing operator (??) replaces verbose and explicit checks for null references in code.

You will find the different options to check whether a value is not null on the image below. And as you can see, the new Null Coalescing Operator can really simplify your code, beautify it and make it more readable.

The operator can also be used to handle SOQL results which can return a single record or can return no records at all. For instance, you would love to find an account by a certain Id but a wrong Id has been passed. It leads to a QueryException. To avoid it, you can also use the new operator and handle this case.

The result of the code is

dont miss out iconDon't forget to check out: Salesforce Winter '23 Flow Updates

However, there are some restrictions on the Null Coalescing Operator:

  • it can not be used as the left side of an assignment operator in an assignment
  • SOQL bind expressions do not support it
  • Support of Randomly Generated UUID v4

Salesforce has introduced a very useful UUID class to generate a version 4 universally unique identifier (UUID). The UUID is generated using a cryptographically strong pseudo-random number generator and is represented as 32 hexadecimal values.

The class provides several methods which are listed below:

  • randomUUID(): randomly generated a UUID that uniquely identifies an object
  • equals(obj): compares the UUIS instance with the specified object
  • hashcode(): return the hashcode corresponding to the UUID instance
  • fromString(string): returns a UUID instance from a string representation of a UUID
  • toString(): return the string representation of the UUID instance

Example of code:

  • Make Callouts after Rolling Back your DML

Previously callouts after creating savepoints resulted in a CalloutException regardless of whether there was uncommitted DML or the changes were rolled back to a savepoint. Now you can use the new Database.releaseSavepoint() method to explicitly release savepoints before making a callout and roll back all uncommitted DML to a specific savepoint.

Roll back all uncommitted DML by using a savepoint.

  • Evaluate Formula at Apex Runtime

This feature is currently available for developer preview only. However, I found it very useful as it allows us to evaluate user-defined dynamic formulas for Apex objects and Sobjects without using database CPU.

For now, this feature is available in scratch orgs only where the FormulaEvalInApex feature is enabled. If it isn’t enabled, Apex code can be compiled but not executed.

To create an instance of the formula, call the static method builder() in the FormulaBuilder class by specifying the formula text and context object. To validate the formula instance, call the build() method. The FormulaValidationException exception is thrown if validation fails.

To calculate the formula expression and return the result, use the evaluate() method in the FormulaInstance class. The FormulaEvaluationException exception is thrown if validation fails.

Here’s an example that uses the mentioned methods:

  • example 1:
  • example 2:

In my opinion, this is a very useful and convenient feature and hopefully, it’ll become available in all orgs in a while.

dont miss out iconCheck out another amazing blog here by Vimera: Einstein Prediction Builder: Next Steps to Build an Automated Flow

To continue reading, please visit our website.

Responses

Popular Salesforce Blogs