Month: June 2015

Implementing Decorator Design Pattern in Apex

This post is part of the series – Design Pattern in ApexI am sure as a developer, we all faced some situations where we need to have some kind of temporary fields just for calculation or displaying in the UI, but we don’t want to store that information in the database or more specific in object. To solve this type of problems, we have Decorator Design Pattern, which allows a user to add a new functionality to an existing object without modifying it’s internal structure. This pattern create a decorator class which wraps the original class and provides additional functionalities keeping class method signature same.Where I should use this design pattern?Below are few user cases, where we can use this design pattern -Say in a table, we are displaying a list of records and want to perform some operations on selected records. We can perform the same operation on each record, but from user interaction point of view, if we can provide some check-boxes so that user can select multiple records inside the table and perform the same operation on all the selected records. We can display check-box against each row, but that check-box information we will not store into the object level, these are only for Visual Force pages.Say when we are creating new accountss we are passing the same account information to some MDM (Master Data Management) system via integration. Once the...

Read More

Implementing Strategy Design Pattern in Apex

This post is part of the series – Design Pattern in ApexThe strategy design pattern is the one which allows an algorithm’s behavior to be chosen at run time. It is used to define a set of algorithms to solve a common problem. It allows unique logic per algorithm via encapsulations, but ensures that all algorithms are interchangeable at run time. Here the abstraction is achieved by an interface, whereas individual implementations are done at derived classes. So basically it looks like -Where I should use this design pattern?This design pattern should be used when you are planning to perform an operation that has a common end goal, but there can be different approaches/ways to achieve that goal. All these approaches can be chosen by client at run-time.Let me tell you few real-life Salesforce based example where this design pattern can be used -Let’s say in the account detail page, you want to display the current temperature of the city where the office located. Now based on the country/state, you may need to collect the temperature information from different websites, but your final goal is always the same i.e. showing the temperature.Let’s say you are calculating discounts per product based on month. Every month, the discount amount is getting calculated by different complex algorithms/formulas. Here also, your final goal is same i.e. calculating the discounts.Let’s say you want to set...

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 (25)
  • 2023 (47)
  • 2022 (25)
  • 2021 (35)
  • 2020 (41)
  • 2019 (13)
  • 2018 (16)
  • 2017 (14)
  • 2016 (18)
  • 2015 (74)
  • 2014 (20)