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
Zach DelacroixZach 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.
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


 
Best Answer chosen by Zach Delacroix
Pankaj_GanwaniPankaj_Ganwani
Hi 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:
 
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

             if(!ObjMap.containskey(Obj.Account__c))
                  ObjMap.put(Obj.Account__c, new List<CustomOBJ__c>{Obj});
             else
                  ObjMap.get(Obj.Account__c).add(Obj);

         }

 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi 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:
 
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

             if(!ObjMap.containskey(Obj.Account__c))
                  ObjMap.put(Obj.Account__c, new List<CustomOBJ__c>{Obj});
             else
                  ObjMap.get(Obj.Account__c).add(Obj);

         }

 
This was selected as the best answer
Zach DelacroixZach Delacroix
Thanks Pankaj!

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
 
Pankaj_GanwaniPankaj_Ganwani
Hi,

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);
}
Zach DelacroixZach Delacroix
Hi Pankaj,

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