Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to get values from a map apex?

Asked by Ai Sep 23, 2022 9.3K views 3 answers
Share

About this question

I have a map passed as a parameter of an apex method. In this method I need to get the values of the map. Here is the apex method :


@AuraEnabled
public static String manageFilters(Map lines){
    System.debug('### lines : ' + lines);
    System.debug('### lines.values() : ' + lines.values());
    for(String key : lines.keySet()){
        System.debug('### lines.get(key) : ' + lines.get(key));
        System.debug('### >>> ' + lines.get(key));
    }
    return null;
}
The map lines is not null and contains some values, here is what I can see in the system debug of lines :
{0={FieldName=AccountId, ObjectName=Opportunity, Operator=<, Value=dfg}}


(Potentially, there could be more than one value)


My question is, how can I get, for example, the value of the 'ObjectName' or the value of the 'FieldName' of this map ?

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 12, 2025

If you’re working with maps in Apex and wondering how to retrieve values, there are several ways to do it. Apex Maps work as key-value pairs, similar to Java’s HashMap. Here’s how you can extract values efficiently:

1. Get a Single Value Using a Key

The simplest way to get a value from a map is by using the .get() method:

Map scoreMap = new Map();
scoreMap.put('Alice', 95);
scoreMap.put('Bob', 88);
Integer aliceScore = scoreMap.get('Alice');  // Returns 95

If the key doesn’t exist, get() returns null.

2. Check if a Key Exists Before Retrieving a Value

Avoid null errors by checking if the key is in the map:

if (scoreMap.containsKey('Bob')) {
    Integer bobScore = scoreMap.get('Bob');
    System.debug('Bob's Score: ' + bobScore);
}

This ensures you don’t try to fetch a value that’s not there.

3. Get All Values from a Map

If you need all values, use .values():

  List allScores = scoreMap.values();

This returns a List of all values in the map.

4. Iterate Over Key-Value Pairs

If you need both keys and values, loop through the map:

for (String student : scoreMap.keySet()) {
    System.debug(student + ' scored ' + scoreMap.get(student));
}

This retrieves each value dynamically.

Final Thoughts

Apex Maps make it easy to store and retrieve data efficiently. Whether you need a single value, all values, or to loop through keys, these methods will help.


Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 23, 2024

In Apex, you can get values from a map using the get() method or by directly accessing the map keys. Here are examples of both approaches:


Using the get() method:Map myMap = new Map();
myMap.put('key1', 10);
myMap.put('key2', 20);
Integer value1 = myMap.get('key1');
System.debug('Value for key1: ' + value1); // Output: Value for key1: 10
Integer value2 = myMap.get('key2');
System.debug('Value for key2: ' + value2); // Output: Value for key2: 20

Directly accessing the map keys:

Map myMap = new Map();
myMap.put('key1', 10);
myMap.put('key2', 20);

Integer value1 = myMap.get('key1');
System.debug('Value for key1: ' + myMap.get('key1')); // Output: Value for key1: 10
Integer value2 = myMap.get('key2');
System.debug('Value for key2: ' + myMap.get('key2')); // Output: Value for key2: 20

Both approaches will retrieve the value associated with the specified key from the map. If the key is not found in the map, the get() method will return null, so make sure to handle such cases appropriately in your code.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.