You need to sign in to do that
Don't have an account?
Abilash 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 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())
{
System.Debug(field.getDescribe().getName());
}
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){
try{
fldValues.put(fld, string.valueOf(accountResults[0].get(fld)));
} catch (exception e){
system.debug(fld);
}
}
http://forceguru.blogspot.in/2012/11/page-block-table-with-dynamic-columns.html
* use the dynamic Soql part of this code and use it for your needs