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
Shravan Kumar 71Shravan Kumar 71 

Accessing fields of Account from Map<String, List<Account>>

Hello There,

I have a Map i.e., Map<String, List<Account>> emailAccListMap
I am trying to access the account field from the above Map by saying emailAccListMap.get(String).Name, but this doesn't work.

Can anyone help?

Thanks
Best Answer chosen by Shravan Kumar 71
Lokesh KumarLokesh Kumar
It won't work because the values of the map is a list not object. You need to traverse the emailAccListMap.get(String) and then access the name

like 
for (Account  acc : emailAccListMap.get(String)){
acc.name;
}

All Answers

Lokesh KumarLokesh Kumar
It won't work because the values of the map is a list not object. You need to traverse the emailAccListMap.get(String) and then access the name

like 
for (Account  acc : emailAccListMap.get(String)){
acc.name;
}
This was selected as the best answer
Raj VakatiRaj Vakati
Try this
 
for (String   s : emailAccListMap.keySet()){

List<Account> accs  = emailAccListMap.get(s);
}

 
Arun ChowdaryArun Chowdary
Hi Shravan,


The solution is simple,  If you want to get Account record from the map then using key we can get it. In your code, You mentioned "string" as a key in the Map. So we can get using Accountname. Rest you can understand with the help of code I have provided below.    


for(account a:trigger.new){

List<Account> acc=emailAccListMap.get(a.Name);

}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Arun Chowdary


 
Shravan Kumar 71Shravan Kumar 71
Thank you everyone for proving the solution.