Learn Map in Salesforce as a Collection – CoderInMe

What is a Collection?

A Collection is set of related or similar objects or things.
In Salesforce there are 4 types of Collection.but we will discuss here 3 types only.

  1. List: Collection of elements in an ordered way like an array, it can store duplicate elements.
  2. Set: Collection of unique elements in a unordered way.
  3. Map: Map is key – value pairs.
Id/Key Value
1 abc
12 ddd

Map in Salesforce

In salesforce term Map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

Declaration:

Map<ID, String> mapString = new Map<ID, String>();

e.g. 0100000ds—>ab

Map<String, Set<String>> mapset = new Map<String, Set<String>>();

e.g.  a—>{a,ccc,ssss}

Map<String,Account>  mapofAccount =new Map<String,Account>();

Note :Apex uses a hash structure for all maps. No size limit but, heap size of salesforce is considerable.

From salesforce.com you can see the following

Map Constructors
  1. new map of data type string
  2. standard object map
Map Methods
 — get() method is used to retrieve the value from map for a particular key.
Account ac= accountMap.get(‘saifi’);

Now you are familiar with Map.

Let’s solve one problem using Map.  Write a trigger for Every time a lead is created, an account will be created or updated on the basis of email. if lead’s email is in any existing account’s email then trigger will update the account name to lead’s  last name, if lead’s email doesn’t belong to any account then the new account should be created with name as lead lastname and email as a lead’s email.

If we will code it using for loop and list,Map in Salesforce

but it’s not optimized code due to nested for loop.

In the above code what I did that I wrote a simple trigger which will work on before insert. this means whenever a new lead will create this trigger will execute.we will use a map like following then you can see how the code is good optimized.

trigger emailAcc on Lead (before insert) {
    set <string> emailset=new set <string>();
    for(lead ld: Trigger.New){
        if(ld.email!=null){
            emailset.add(ld.email);
        }
    }
    
    Map<String,Account> emailmap=new Map<String,Account>();
    if(emailset!=null && emailset.size()>0){
        list<Account> aclist=[Select Id,name,email__c from Account where email__c in:emailset];
    
    if(aclist.size()>0){
        for(Account a: aclist)
            emailmap.put(a.email__c,a);
        }
    }
    
    list<Account> upsertlist=new List<Account>();
    for(lead ld: Trigger.New){
        Account acc = new Account();
        if(emailmap.size()>0 && emailmap.containsKey(ld.email)){
            acc = emailmap.get(ld.email);
            acc.name=ld.LastName;
            upsertlist.add(acc);
        }else{
            acc.Name= ld.LastName;
            acc.email__c = ld.email;
            upsertlist.add(acc);
        }
    }
    if(upsertlist.size()>0){
        upsert upsertlist;    
    }
}

we will discuss it soon, till then learn code and enjoy.

Video tutorial for this article is also available

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.