Category: use case

How to update “N” records in Flow

 If you are implementing business logic through Salesforce’s flow, you might need to loop through a list of records to do some manipulation. This is a very basic requirement. If you ever need to query records from Salesforce (let’s say all accounts where the billing country is India), it is very easy to do in SOQL and Get Records flow elements. But things will become a little tricky when the requirement says “Fetch first 5 accounts based on the created date where the billing country in India”. It is definitely very easy in SOQL with the limit statement, but in Get Records flow element, there is no way to (as of Winter’21 release) limit the record.To overcome this situation, we can implement the counter mechanism which will basically make sure that when the # of loop iteration is greater than 5 (because we just need the first 5 accounts), the flow will not perform any business logic.Here is the overall flow -Here as you can see for each loop iteration, we are incrementing the counter value by 1 and then checking whether the new value is greater than 5 or not. Based on that decision element, either we are updating the account’s rating i.e. performing business logic or ignoring the business logic execution. This flow solution is definitely making sure that only for the first 5 records the business logic...

Read More

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

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
Loading