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
Abilash Kosigi 8Abilash Kosigi 8 

Retrieving all field values of Accont object into a map

Can any one please share the code on how to retrieve all the field values of Account Object with Field names as Map Keys. This has to be done in apex class.
I think if you are asking how to get the records in map by query then 
Map<id,Account> acctMap = [select id, ( and all your fields in account object ) from  Account limit 100];

Abilash Kosigi 8Abilash Kosigi 8
Thanks for your reply :) Do I have to know the entire list of fields to write in the query. Is there anything sort of 'select * from ' here, where I dont have to write all the fields in the query
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
In salesforce we cannot use "Select * from".If you dont know about fields in the aaccount object. Then Go for dynamic Soql.In Schema class there is get describe() method to get fields from the object dynamically.Use it according to your requirement.
Abilash Kosigi 8Abilash Kosigi 8
Hi Vidhyasagaran, Can you please give the syntax of describe method in the above sql statement.  

I know this statement gives all the fields list of an object. In this case is Car__C.  However, I do not know how to use in SQL Statement. It would be great if you can help with this. And also, how to display the values in for loop.
Map<String, Schema.SObjectField> fieldsMap = Schema.SObjectType.Car__C.fields.getMap();

for (Schema.SObjectField field : fieldsMap.values())
Jim JamJim Jam
try this as a guide, adapt to your needs ..

Account account = new Account();
// get all account fields from database schema
Schema.SObjectType objectType = account.getSObjectType();
set<String> accountFields = objectType.getDescribe().fields.getMap().keySet();

// construct soql as required, including all account fields
string soql = 'select ';
for (string fld :accountFields){
    soql += fld +',';
soql = soql.substring(0, soql.length() - 1);

soql += ' from account ';
// and any where criteria ..   
list<Account> accountResults = database.query(soql);
map <string, string> fldValues = new map<string, string>();
// put field values of first record into a map..
for (string fld :accountFields){
     fldValues.put(fld, string.valueOf(accountResults[0].get(fld)));
    } catch (exception e){
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
See this blog he has done for select object and display field names of particular object and diplay them  in the page block table use this code as example and change it for you need
* use the dynamic Soql part of  this code  and use it for your needs