Category: 501


This post is part of the series – Design Pattern in ApexI know it was discussed number of times earlier about Apex Trigger Design Pattern. Below are few excellent posts - have learned a lot from the above two posts by Steve Andersen and Mike Leach.But the post by Abhinav Gupta really inspires me a lot and I am going to explain the pattern/template here with few use cases.The motivations for this design pattern are -Write Trigger with minimum codeFollow Salesforce Best Practice – One Trigger per objectFollow another Salesforce Best Practice – Move all code from Trigger to Apex Classes – Handler ClassesAbility to add and order multiple handlers for the same trigger event.Easy to maintain code base.Here is the Apex Trigger template – MyTriggers (Again inspired by Abhinav Gupta)Now using the above template, below is the account trigger handler – AccountTriggerHandlerAnd finally the trigger – AccountTriggerNow the trigger code is very neat and clean. At the same time all our operations against trigger events are moved to handler class.So now let’s consider few use cases -Use Case 1 -While inserting new accounts, if the Account Rating is marked as ‘Hot’, then mark those accounts as Active and make the Customer Priority as ‘Medium’.Implementation -To implement the above requirement, what we need to do is simply add the functionality in AccountTriggerHandler.AccountBeforeInsertHandler class. No changes in Apex trigger code is required.Here is the updated AccountTriggerHandlerUse...

Read More


Let me discuss about another use case where it is required to pass information from one VisualForce page to another VisualForce page while both the VisualForce pages are sharing the same controller. I know this is a very basic example, but I would like to include this use case as part of my previous post – HOW TO PASS PARAMETERS TO VISUALFORCE PAGES AND BETWEEN PAGES. Special thanks to Joseph McNulty for pointing this to me.Check out my youtube channel for more Salesforce videos - about Salesforce Release Spring 21 here.To start with let us first create the common controller – SingleControllerHere is the first VisualForce Page – FirstPage.Here is the second VisualForce Page – SecondPage.As you can see from FirstPage, I am setting the name to the controller variable – name and then clicking on the commandButton,  goToSecondPage() method from controller is getting called. This goToSecondPage() method is returning a PageReference to the SecondPage. The point to notice here is that inside the method goToSecondPage(), I am making the setRedirect(false). I will explain what will happen if I set it to true later in this post. So what is happening after clicking on the button is that a SecondPage is getting displayed with Welcome message. But another interesting thing to notice here is that URL is not getting changed i.e. URL is still pointing to /apex/FirstPage though the content...

Read More

How to use Salesforce Describe Methods – Record Types, Fields, and Global Describe

 In this post, I will try to explain how to use Salesforce Describe Methods to fetch record types, fields etc.Record Type Describe -If you want to fetch an object’s RecordType ID, you don’t need to do SOQL operation. There is a method called getRecordTypeInfosByName is there for you. Don’t go by the name of the method, as this method will return you the record type id searching by label, not by name. The label is case sensitive. So if the label does not match the exact spelling and case, a null pointer exception will be thrown. So as a developer, you need to add proper error handling mechanism.When you have more than one record type then you can put the record type information into a map. Below is the code to do that -sObject Field Describe -This will return a map of field tokens for an sObject type and then the field describe method can provide a number of useful information about that field. With this, you can make your code flexible to work for any object. Below is the code -You can refer to the Salesforce Documentation for all the methods . Click HereGlobal Describe -This will return a map of sObject tokens for all sObjects. Below is the method -Note – Fetch sObject Type from Record IdThe Schema.SObjectType can be retrieved from a Record Id. This is...

Read More

Implementing Singleton Design Pattern in Apex – Use Case III

This post is part of the series – Design Pattern in ApexToday 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....

Read More

Implementing Singleton Design Pattern in Apex – Use Case II

This post is part of the series – Design Pattern in ApexToday in this post I will explain how Singleton Design Pattern can be used to stop calling the same trigger twice.Consider the situation – You as a developer, is updating a field in Trigger which in turns calls a workflow and in that workflow action you are again updating some other field from the same object. And to your surprise, the same trigger is getting triggered again. Getting confused, right?Let’s become more specific with the situation.Consider the below object – Airport__cThe requirements are -Req 1: Send an email if the country and type of an airport changed.Req 2: If the country of an airport changed to “India”, change the type to “International”. To achieve the above two requirements, we have implemented -One trigger, which will send the email if the country and type of an airport changed.One workflow, which will change the type to “International” (Workflow action) if the country of an airport changed to “India”.Here is the trigger -The trigger looks really simple. But you know what will happen, when you will change the country of an airport to “India”, you will get two emails. You know why?First email you will get from Line#6. After that, workflow action will change the type of the airport to “International”. Due to this change you will get another email from Line#11.To solve...

Read More

Earlier Posts

  • 2024 (28)
  • 2023 (47)
  • 2022 (25)
  • 2021 (35)
  • 2020 (41)
  • 2019 (13)
  • 2018 (16)
  • 2017 (14)
  • 2016 (18)
  • 2015 (74)
  • 2014 (20)