Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Converting List to map

I have a query that

List OList = [select id,name,fld3,fld4,fld5 from Opportunity]

i need to add field name like 'id,name,fld3,fld4 etc,.. as map keys and results of the fields as map values

List<Opportunity> OList = [select id,name from Opportunity]

    like 
    [
    id = Olist.id,
    name = Olist.name
    .....

    ]

Is it possible to make like that?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Have I understood your question correctly?

1: i need to add field name like 'id,name,fld3,fld4 etc,.. as map keys

String fieldNames = String.join(new List<String>{
    SObjectType.Opportunity.Fields.Id.Name, //"Id"
    SObjectType.Opportunity.Fields.Name.Name //"Name"
}, ',');

List<Opportunity> opportunities = Database.query('SELECT ' + fieldNames + ' FROM Opportunity');

2: and results of the fields as map values

You are looking for data structure like this?

{
    "Id": [
        "006d0000008WaWzAAK",
        "006d0000008WaWpAAK",
        "006d0000008WaXHAA0"
    ],
    "Name": [
        "Pyramid Emergency Generators",
        "Dickenson Mobile Generators",
        "Grand Hotels Emergency Generators"
    ]
}

You can use a Map<String,List<Object>> data structure to hold all the field values. Then convert the SObject into a Map itself to loop over its fields easily. Here's a code sample that yields the above:

//select any fields you want
List<Opportunity> opportunities = [SELECT Id, Name FROM Opportunity]; //or from above

//prepare the data structure to hold the list of values
Map<String,List<Object>> field2values = new Map<String,List<Object>>();

//every opportunity
for (Opportunity opportunity : opportunities) {
    //we can convert any SObject into a Map
    Map<String,Object> mapOpp = (Map<String,Object>)Json.deserializeUntyped(Json.serialize(opportunity));

    //every field
    for (String key : mapOpp.keySet()) {

        //leave out the metadata
        if (key == 'attributes') continue;

        //list of values needs to be initialized the first time around
        if (field2values.get(key) == null) field2values.put(key, new List<Object>());

        //populate each value
        field2values.get(key).add(oppMap.get(key));
    }

}

System.debug(Json.serializePretty(field2values));
share|improve this answer
1  
Thank u fnd..this is what exactly i wanted –  Sunny Jan 31 at 11:04

You can use the dynamic DML syntax to retrieve fields from an sobject based on another variable. For example:

List<Opportunity> myList=[select id, Name from opportunity];

String keyField='id';
String valField='Name';

Map<Id, String> oppsMap=new Map<Id, String>();

for (Opportunity opp : myList)
{
  oppsMap.put( (Id) opp.get(keyField), (String) opp.get(valField) );
}
share|improve this answer
    
But my pbm is id,name no longer exists ,after 10 min the fields chnages to firstname,lastname, the fields we are retreiving are not same –  Sunny Jan 31 at 9:11
    
You'll need to give more information on your scenario I'm afraid - you can't expect to retrieve values that you haven't stored. –  Bob Buzzard Jan 31 at 9:13
    
Yeah sure bob, i have the fields already in object after retreving the results with query we need to make fields name which was specified in the query should come under map keys and corresponding results as values. Is it possible –  Sunny Jan 31 at 9:27
    
Its not possible to store data in a map using one key and then retrieve it using another key. If you know what all the possible keys are you could store in multiple maps. –  Bob Buzzard Jan 31 at 9:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.