2

I want to create a map of apiname and value pair..please help... i want record value...like if apiname of opportunity field is name..then i want record value..ie opportunity name...

for eg

Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
Opportunity opp=[Select id,name,accountId from Opportunity];
 Map<String,string> valueMap=new Map<String,String>();

    for(opportunity o:opp)
           {
                for(String fieldName: fieldMap.keySet())
                {
                    **valueMap.put(fieldName,o);**
                }

           }

3 Answers 3

2

OK, having re-read the question...

You can use get with the fieldname on the sObject, e.g.

Opportunity opp=[Select id,name,accountId from Opportunity limit 1];
System.debug(opp.get('name'));

So in your example I think you want

valueMap.put(fieldName, o.get(fieldName));
4
  • thanks for replying doug..but i want record value...like if apiname of opportunity field is name..then i want record value..ie opportunity name...
    – miku
    Commented May 18, 2013 at 12:36
  • I have edited the answer - does this help?
    – Doug B
    Commented May 18, 2013 at 12:46
  • great..that's perfect...i wanted this exactly ...great..thanks..
    – miku
    Commented May 18, 2013 at 13:21
  • No problem, glad to help :)
    – Doug B
    Commented May 18, 2013 at 13:22
2

You can do something like this

Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
Opportunity opp = [Select id,name,accountId from Opportunity];
Map<String,String> valueMap = new Map<String,String>();

for ( Opportunity o : opp ) {
    for ( String fieldName : fieldMap.keySet() ) {
        valueMap.put(fieldName, o.get(fieldName));
    }
}
0
1

I see that you have already accepted an answer, but I think there is a flaw in your example code, if you have more than one opportunity in the opp list your will end up with only key/value pairs in the valueMap of the last opportunity in the list.

For example if you have 2 opportunities {opp1(Name="Greg"), opp2(Name="Tom")} in the list, your Map will look like {Name=>"Tom"}

Another point is that Apex SObjects already have Map methods for accessing the values on the object. I refer to the SObject Methods Documentation. There are get and put methods that would accomplish everything your key value Map does. Both other answers are using these methods to populate the valueMap

2
  • It's a good point, and it would be interesting to understand what the purpose of the code actually is. Also the query is only returning id, name, accountid from the opportunity so most of the opportunity data won't be there to query. Still I think the answer stands as "how can I generically get sObject values"
    – Doug B
    Commented May 19, 2013 at 7:23
  • good point daniel... actually i wont get more than 1 opportunity...as my query will have where clause Opportunity opp = [Select id,name,accountId from Opportunity where id=:oppid];' anyways thanks for the point..
    – miku
    Commented May 19, 2013 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .