You need to sign in to do that
Don't have an account?
Zach Delacroix
Question with Map Collection
Hi Guys!
I have a question about Populating value of the Map. While doing some test today, I noticed that Map returns different value.
Here's an example:
Cobe below gives me just 1 value. But When I look on the Account Related list, there are more than 5 records that meets my query.
This one below shows correct Value. Shows all five records under the Account Related List. I simply changed ObjMap.put(Obj.Account__c+Field1, Obj); when populating the Map with the customOBJ records.
My expected Result it that is I have 5 records under Account, and if I do query like the one above, it will pull up all 5 records.
but I only need ObjMap.put(Obj.Account__c, obj); NOT ObjMap.put(Obj.Account__c+Field1, Obj);
This can be helpful on my learning with apex code. Can anyone help with explantion on this please.. Thanks!
-Zach
I have a question about Populating value of the Map. While doing some test today, I noticed that Map returns different value.
Here's an example:
Cobe below gives me just 1 value. But When I look on the Account Related list, there are more than 5 records that meets my query.
Map<String,CustomOBJ__c> ObjMap = new Map<String,CustomOBJ__c>(); For(CustomOBJ__c Obj: [SELECT Id, Name, Account__c, Field1, Field2 FROM CustomOBJ__c WHERE Account__c IN: AccountId]){ //<<This is my Set Account ID ObjMap.put(Obj.Account__c, Obj); } for(String R: ObjMap.keySet()){ System.debug(ObjMap.get(R).Name); }
This one below shows correct Value. Shows all five records under the Account Related List. I simply changed ObjMap.put(Obj.Account__c+Field1, Obj); when populating the Map with the customOBJ records.
Map<String,CustomOBJ__c> ObjMap = new Map<String,CustomOBJ__c>(); For(CustomOBJ__c Obj: [SELECT Id, Name, Account__c, Field1, Field2 FROM CustomOBJ__c WHERE Account__c IN: AccountId]){ //<<This is my Set Account ID ObjMap.put(Obj.Account__c+Field1, Obj); } for(String O: ObjMap.keySet()){ System.debug(ObjMap.get(O).Name); }
My expected Result it that is I have 5 records under Account, and if I do query like the one above, it will pull up all 5 records.
but I only need ObjMap.put(Obj.Account__c, obj); NOT ObjMap.put(Obj.Account__c+Field1, Obj);
This can be helpful on my learning with apex code. Can anyone help with explantion on this please.. Thanks!
-Zach
You should be using Map<String,List<CustomOBJ__c>> ObjMap = new Map<String,List<CustomOBJ__c>>(); instead.
In for loop you can do something like this:
All Answers
You should be using Map<String,List<CustomOBJ__c>> ObjMap = new Map<String,List<CustomOBJ__c>>(); instead.
In for loop you can do something like this:
It works great. But how do I get the value of for example Name field?
in a Simple map, I can get it by doing System.debug(ObjMap.get(Key).Name); but I get Error "Initial Term of field expression must be a concrete SObject: List<customObj__c>"
How do I get field value of the list in the Map then?
Thank you!
-Zach
You can use below mentioned code for doing this:
for(Id accountId : ObjMap.keyset())
{
for(customObj__c obj : ObjMap.get(accountId))
system.debug('==============='+obj.Name);
}
This is awesome! I tried it with my codes and it run Smoothly. I'm now able to get expected Results.. :)
You're awesome!
Thanks a Lot!
-Zach