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
GauravGargGauravGarg 

How to create Map<String, String> from Account List?

Can we convert List<account> to Map<String, String> i.e. Map<FieldLabel, FieldValue> pair. 


Thanks in advance. 
Best Answer chosen by GauravGarg
GauravGargGauravGarg
I got the answer by using Describe call. 
 
List<Account> accList = new List<Account>();

Map<String, Object> AccMap = accList.getPopulatedFieldsAsMap();

Thanks. 

All Answers

Raj VakatiRaj Vakati
Try this
 
List<Account> acc=[Select id ,Name form Account] ;

Map<String,String> str = new Map<String,String>() ;
for(Account a :acc){
  str.put(a.Name ,'Name');  
}

 
PawanKumarPawanKumar
There is no direct way to do so. the only way to iterate your List<Account> and you can build your own map. 

Please let me know your actual requirements then we can help you better.
GauravGargGauravGarg
I got the answer by using Describe call. 
 
List<Account> accList = new List<Account>();

Map<String, Object> AccMap = accList.getPopulatedFieldsAsMap();

Thanks. 
This was selected as the best answer
PawanKumarPawanKumar
List class do not have any method called 'getPopulatedFieldsAsMap()'. so it is wrong. 

describe is used for getting metadata value, not the record value. I do not think your questions & answer is right.
 
PawanKumarPawanKumar
i think you mean the below. and the method on Sobject( not on List)

https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_apex_sobject_getmap.htm