3.5 Use map to bulkify code

This is the explanation part of the intermediate trigger. The scenario and code is at this post.

The grammar in the last post is not very new. It shouldn’t be too hard to read if you follow all the previous chapters. However, you might find it really hard to implement such a trigger. I will explain in this post and the upcoming one. This post will explain why we want to use a map. 

Unbulkified Version

To understand why we want to use map here, let’s think about how we are going to implement the trigger without using Map.

We might end up with something like this:


trigger TransactionTrigger on Transaction__c (after insert) 
{
    for(Transaction__c curTrans: Trigger.New)
    {
        Merchandise__c mer = [Select    Id, Name, 
                                        Overall_Revenue__c,
                                        Discounted_Price__c 
                                  From  Merchandise__c
                                  Where Id = :curTrans];
        if(mer.Overall_Revenue__c == null) 
        {
            mer.Overall_Revenue__c = mer.Discounted_Price__c * curTrans.Amount__c;
        } 
        else 
        {
            mer.Overall_Revenue__c += mer.Discounted_Price__c * curTrans.Amount__c;
        }
        update mer;
    }
}

A little bit explanation about above code

There are still two small grammars to talk about in the above example. First, if we want to use a Apex variable in where clause, we need to add a : before that variable. As shown in Line 9 in the above code.

Also, if a field has no value in Salesforce, it is usually default to null. That’s why we need to check the value before using it in Line 10.

Issue with no Map solution

The above code is easy to think of, but ugly. Please note that both a SOQL and a dml statement are inside the for loop. As we have mentioned before, neither of them should be.

It is easy to avoid the DML one by using a list. However, the SOQL one is harder. We need to understand:

  1. Which set of Merchandise object we need to retrieve. This can be achieved by using a list. So this will be a separate for loop and retrieve the initial list. Then use the list to do the SOQL.
  2. After that, we need to understand the mapping relationship between the elements in the first loop and in the second loop.

Now we understand why do we want to use Map here. Because this is the concept of Map – Finding the related elements by using a key.

In Apex programming to serve the purpose of bulkification, we usually use Id fields as the key. It is not required, but we usually use that.

Now, return to our code and understand it again.

 


trigger TransactionTrigger on Transaction__c (after insert) 
{
    List<String> relatedMerchandiseIdList = new List<String>();
    for(Transaction__c curTrans: Trigger.New)
    {
        relatedMerchandiseIdList.add(curTrans.Merchandise__c);
    }
    
    Map<Id, Merchandise__c> merMap = new Map<Id, Merchandise__c>([Select Id, Name, 
                                                                 		Overall_Revenue__c,
                                                                 		Discounted_Price__c 
                                                                  From  Merchandise__c
                                                                  Where Id in :relatedMerchandiseIdList]);
    for(Transaction__c curTrans: Trigger.New)
    {
        Merchandise__c mer = merMap.get(curTrans.Merchandise__c);
        if(mer.Overall_Revenue__c == null) 
        {
            mer.Overall_Revenue__c = mer.Discounted_Price__c * curTrans.Amount__c;
        } 
        else 
        {
            mer.Overall_Revenue__c += mer.Discounted_Price__c * curTrans.Amount__c;
        }
    }
    update merMap.values();
}

Next Post

3.6 The difference between before and after trigger

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *