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
Andrew SturtAndrew Sturt 

Get a value from a field of an sObject in a list into a map

I'm trying to get the values of certain attributes from a list into a map. The first bit of code below is not my actual code, I'm just trying to explain what I'm trying to do so I can find out if it's actually possible in APEX.

Assuming the list has more than one field in it (in my example below there are four: ID, Name, MyValue, and OwnerId ), is there a way to reference the ID and MyValue fields so I can insert them into a map rather than inserting the whole sObject? In the real code, my list is passed in from another class and I'd like to put it into a map with just the ID and one field.
List<MyObject__c> l = [SELECT ID, Name, MyValue, OwnerId FROM MyObject__c]; 

// I can do this...
Map<ID, MyObject__c> m = New Map<ID, MyObject__c>(l);  

// What I want to do is this...
Map<ID, String> m = New Map<ID, String>(l.ID, l.MyValue); 

As I know someone will ask for it, here's the actual code I'm trying to replace:
Map<ID, String> BenStatusMap = new Map<ID, String>();
        for(Commitment__c CurrRec: lstNewRecords) {
            BenStatusMap.put(CurrRec.Fund_Id__c, CurrRec.Status__c);
        }

First it creates the map, then it iterates through the list to populate the map. This seems very wasteful to me, and I can't help but think there must be a way to do this with less code. That's my goal, to do the same thing with less code.

Can anyone help me?
Best Answer chosen by Andrew Sturt
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Hi Andrew, I think that is the best way to fill map sorry I am pretty sure.

Check the Map class in developer guide then will  come to know if there is any other better way to do this.

Thanks

All Answers

venkat-Dvenkat-D
You can avoid creating list , for loop by accessing the status value wherever required with below code
Map<ID, MyObject__c> m = New Map<ID, MyObject__c>([Select id from MyObject__C]); 
wherever you need status__c simply use below code
m.get(Id__c).Status__c;

I do not think there is way to get exactly what you are asking
Andrew SturtAndrew Sturt
Venky,

As I mentioned in my post, "In the real code, my list is passed in from another class..." So, your suggestion doesn't help much. Sorry. The list is unavoidable. That being the case, is there a way to take two values from the list and put them in a map without using a loop?
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Hi Andrew, I think that is the best way to fill map sorry I am pretty sure.

Check the Map class in developer guide then will  come to know if there is any other better way to do this.

Thanks

This was selected as the best answer
Andrew SturtAndrew Sturt
Thank you, Bhaswanthnaga. That is what I needed to know. I also talked to a contractor working for my employer, and she confirmed that the loop is the only way to do this.