You need to sign in to do that
Don't have an account?
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.
As I know someone will ask for it, here's the actual code I'm trying to replace:
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?
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?
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
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
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?
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