function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lakshmi SLakshmi S 

Map scenario in Salesforce

Hi Team,

What are the uses and how to reduce the code using map in Salesforce?
Give me some scenarios about Map (When & Where we have to use Map in Coding)?


Regards
Lakshmi
SandhyaSandhya (Salesforce Developers) 
Hi,

Refer below salesforce guide to know about maps.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm 
 
And for the scenario i would suggest you refer below link which has the sample code to expalin.

http://www.sfdc99.com/2014/01/25/use-maps-navigate-across-lists/
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 
Lakshmi SLakshmi S
Hi Sandhya,

Thanks for your reply.
Vinod ChoudharyVinod Choudhary
Hi Lakshmi,

Map is used when we need the key-value pair to achieve our requirements.

You can understand it with below examples:
 
// One giant SOQL query with all potential Case creators (users)
List<User> potentialUsers = // SELECT Id, Email FROM User...

// Create a Map that lets you search for Users by their Email
Map<String, User> emailToUserMap = new Map<String, User>();
for (User u : potentialUsers) {
  emailToUserMap.put(u.Email, u);
}

// Get the matching user by Email in just one line of code!
for (Case newCase : Trigger.new) {
  User creator = emailToUserMap.get(newCase.SuppliedEmail);
  newCase.CreatedById = creator.Id;
}

To access elements in a map, use the Map methods provided by Apex. This example creates a map of integer keys and string values. It adds two entries, checks for the existence of the first key, retrieves the value for the second entry, and finally gets the set of all keys.
 
Map<Integer, String> m = new Map<Integer, String>(); // Define a new map
m.put(1, 'First entry');                  // Insert a new key-value pair in the map
m.put(2, 'Second entry');                  // Insert a new key-value pair in the map
System.assert(m.containsKey(1));  // Assert that the map contains a key
String value = m.get(2);               // Retrieve a value, given a particular key
System.assertEquals('Second entry', value);
Set<Integer> s = m.keySet();       // Return a set that contains all of the keys in the map


Unlike Java, Apex developers do not need to reference the algorithm that is used to implement a map in their declarations (for example, HashMap or TreeMap). Apex uses a hash structure for all maps.

The iteration order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. However, we recommend to always access map elements by key.

A map key can hold the null value.

Adding a map entry with a key that matches an existing key in the map overwrites the existing entry with that key with the new entry.

Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct.

The uniqueness of map keys of user-defined types is determined by the equals and hashCode methods, which you provide in your classes.

The uniqueness of keys of all other non-primitive types, such as sObject keys, is determined by comparing the objects’ field values.


Hope this will help you to understand The Map   :)

Thanks
Vinod
Lakshmi SLakshmi S
Hi Vinod,

Thanks for your reply
Lakshmi SLakshmi S
Hi Sandhya,

Do you have any idea on pre-populating the field values while creating new record without any relation ship from those two objects ?
My scenario is create a button on event detail page, button name is New Opportunity. I want to attach that activity to newly creating opportunity.
Could u plz give me any idea on this scenario. (It will be work on both classic and lightning environment)

Regards
Lakshmi