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
venturecventurec 

Iterate over a Map using a for loop

Does salesforce Apex support iterating over a map using a for loop?
Best Answer chosen by Admin (Salesforce Developers) 
CaptainObviousCaptainObvious

You can also use the values method:

 

Map<ID,Target__c> mapTargets = new Map<ID,Target__c>([ Select Id, Name From Target__c Where Account__c in:TargetAccountIDs]); for(Target__c target : mapTargets.values()) { System.debug('*************** Current Target: ' + target.Name); }

 

(The returned list is also in arbitrary order)

All Answers

NOTSOFASTNOTSOFAST

I don't think you can iterate through the contents of a Map directly. You can put the Map contents into a Set or List and then do your loop.

 

        Schema.DescribeSObjectResult objSchema = Account.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = objSchema.fields.getMap();

        for (String fieldName : fieldMap.keySet()){

            System.debug('field name is ' + fieldName);
        }

 

Hope that helps.

venturecventurec

Thanks. I'll use that method.

 

Appreciate the response.

Anand@SAASAnand@SAAS
You could also use  the "keySet()" method to iterate over keys in the map and fetch the value.

Ma<String,Account> mapAccounts = new Map<String,Account>();for(String key:mapAccounts.keySet()){ System.debug(mapAccounts.get(key));}

 THis does not gurantee the order in which the keys would be returned.

CaptainObviousCaptainObvious

You can also use the values method:

 

Map<ID,Target__c> mapTargets = new Map<ID,Target__c>([ Select Id, Name From Target__c Where Account__c in:TargetAccountIDs]); for(Target__c target : mapTargets.values()) { System.debug('*************** Current Target: ' + target.Name); }

 

(The returned list is also in arbitrary order)

This was selected as the best answer
venturec35venturec35

mapName.values() is what works.

 

thanks for the help.

BroncoBoyBroncoBoy
Just want to add,  here's what iterating over a key set looks like:

list<account> myAccounts = new list<account>();
myAccounts = [Select ID, Name from Account limit 10];

map<id, string=""> myAMap = new map<id, string="">();
for ( Account a : myAccounts ){
    myAMap.put(a.ID, a.Name);
}

for ( ID aID : myAMap.keySet() ){
    system.debug(loggingLevel.debug, myAMap.get(aID));
}

Taken from:  http://www.sundoginteractive.com/sunblog/posts/apex-why-maps-are-your-friend
Dhanik Lal SahniDhanik Lal Sahni
Map<Id,Account> acctsWithOpps = new Map<Id,Account>([SELECT Id,(SELECT Id FROM Opportunities) FROM Account]);
System.debug(acctsWithOpps);

for (Account account : acctsWithOpps.values())
{
    if(account.Opportunities.size()>0)
    {
        List<Opportunity> childOpportunities = account.Opportunities;
        System.debug(childOpportunities);
    }
}

 
Priyanka Maheshwari 7Priyanka Maheshwari 7
Hi Dhanik,

In your same code, what if I want to check a field value from opportunity records.
Let say, I need to check status of each opportunity stored in lit childOpportunities. How we can do that without nested for loop.
farukh sk hdfarukh sk hd
Hi,

These posts will cover about list,set,map usage and methods available and how to use map in lightning components,

https://www.sfdc-lightning.com/2018/09/collection-in-salesforce.html

https://www.sfdc-lightning.com/2018/09/how-to-use-map-in-lightning-component.html
hemant ghaturkar 18hemant ghaturkar 18

i have to write trigger and update the records
if i have more than 50000 record in contact object as per gobvernor limit 100 it will throw error how will i achive this senario
padala Venkata Surya Prakashpadala Venkata Surya Prakash
I think this might helps you.

Map<Id,Contact> result=new Map<Id,Contact>([select Name from Contact limit 10]);
System.debug(result);
    for(Contact c:result.values()){
    System.debug(c);
}
David Roberts 4David Roberts 4
The general case is:
Map<Id, myObjectType> mapName = new Map <Id, myObjectType>();
for (Id key: mapName.keyset()){
    myObjectType theObject = mapname.get(key);
    //do something with theObject
}//next key

 
RAMPRASADH NATARAJANRAMPRASADH NATARAJAN

We can use get method like as follows

Map<String,Integer> mapper=new Map<String,Integer>();
mapper.get(key);

 

Adam JamesAdam James
If you are planning to protect your computer using an internet security program, then you must visit webroot.com/safe (https://webr4oot.com) to learn about Webroot security programs. It is a unique security system developed for computers to fight against viruses and internet attacks. Whoever uses Webroot gets the benefit of secured privacy and encrypted data on their computers.
Aman Kumar Mishra 42Aman Kumar Mishra 42
You Can use this way:
Map<Id,Account> accMap = new Map<Id,Account>([Select Id,Name From Account Limit 10]);
        for(Account acc : accMap.values())
        {
            System.debug('Account Id :'+acc.Id+' '+'Account Name : '+acc.Name);
        }