Implementing Singleton Design Pattern in Apex – Use Case III

by Sudipta Deb | Jun 5, 2015 | 501, Apex, design pattern, dev 501, singleton, use case | 0 comments


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.

Written by

Related Posts

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

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!

read more...
Apex Cursor and Its Advantages Over Batch Processing

Apex Cursor and Its Advantages Over Batch Processing

Struggling with large datasets and SOQL governor limits in Salesforce? In this video, I will dive deep into Apex Cursors, the powerful new feature that revolutionizes how we process millions of records.

Apex Cursors allow developers to navigate through query results in manageable chunks within a single transaction or across Queueable jobs. In this video, I will show you the correct way to implement Apex Cursor and also when this is an exception, how to handle that and perform retry.

read more...

0 Comments

Leave a Reply