This post is part of the series – Design Pattern in Apex
Today in this post I will explain how Singleton Design Pattern can
be used to call global describe only once.
Requirement –
When someone inserts an account record, you need to inspect all the fields in the account object.
Implementation –
Below is the trigger to start with –
MyAccountTrigger-
AccountHelper –
The problem with this approach is that when someone enters huge number of accounts with dataloader, this code will work like –
For each insert, it will create a new instance of AccountHelper which in turn will execute GlobalDescribe(). Very soon we will be hit by LimitException.
To avoid that, we have to implement Singleton Design Pattern and below are the changes we need to do that –
AccountHelper –
MyAccountTrigger-
Now you can see for a single transaction, we are calling GlobalDescribe only once because we are now not instantiating AccountHelper object for each Account insert, rather we are only instantiating one AccountHelper object per transaction.
Hope you understood the usages of Singleton Design Pattern. If you have implemented this design pattern in any other situation, requesting you to share here. Thanks for your feedback. Thanks.
New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex
For years, Salesforce developers have faced a common challenge: programmatically retrieving picklist values that are specific to a certain Record Type directly within Apex. The “solutions” often involved either complex SOQL queries against metadata, relying on the UI API (with its associated callout limits and serialization overhead), or maintaining clunky custom metadata/settings.
Good news, Technical Potpourri readers! The Salesforce Spring ’26 release is bringing a game-changer that will significantly simplify your Apex code and improve performance: native Apex methods to filter picklist values by Record Type!





0 Comments