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
Miriam Caldwell 4Miriam Caldwell 4 

Map Method putAll not concatenating

Hi, 
I'm very new to APEX so please bear with me.  I have 2 different map strings objects and I'm trying to concatenate them using the map method .putAll, however it's replacing instead of concatenating.   

if(aresp.getBody() != null ||  bresp.getBody() != null ){
                Map<String, Object> aretVal;
                Map<String, Object> bretVal;
                if(aresp.getBody() != null)
                {
                    System.Debug('****** raw a Response: ' + aresp.getBody());
                    aretVal = (Map<String, Object>) JSON.deserializeUntyped(aresp.getBody());
                }
                System.Debug('****** aretVal: ' + aretVal.size());
                System.Debug('****** aretVal: ' + aretVal);
                if(bresp.getBody() != null)
                {
                    System.Debug('****** raw b Response: ' + bresp.getBody());
                    bretVal = (Map<String, Object>) JSON.deserializeUntyped(bresp.getBody());
                    System.Debug('****** bretVal: ' + bretVal.size());
                    System.Debug('****** bretVal: ' + bretVal);
                }
                
                if(!bretVal.isEmpty())
                    aretVal.putAll(bretVal);


putAll(sobjectArray)
Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.
Suraj Tripathi 47Suraj Tripathi 47

Hi Miriam,

Please find the solution. You can take reference from this code.

Map<String,Object> aretVal = new Map<String,Object>();
        aretVal.put('a','Rohan');
        aretVal.put('b','Mohan');
        Map<String,Object> bretVal = new Map<String,Object>();
        bretVal.put('c','Sohan');
        bretVal.put('d','Geeta');
        bretVal.put('a','Babita');
        aretVal.putAll(bretVal);
        system.debug('aretVal:: '+aretVal);
Or

map<String,Object> aretVal  = new map<String,Object> 
        {'a' => 'Rohan', 'b' => 'Mohan'};
            
            aretVal .putAll(new map<String,Object> 
                             {'c' => 'Sohan', 'd' => 'Geeta', 'a' => 'Babita'});

Or

Map<String,Object> jsonMap = new Map<String,Object>{'root'=> new Map<String,Object>{'field1'=>'value','field2'=>'value'}};
System.debug(JSON.serialize(jsonMap));

Your parent map key is root, so if you wanna add field3, you can

((Map<String,Object>)jsonMap.get('root')).put('field3','value');

If it helps please mark it as Best Answer so that other people would take reference from it.

Thank You

Miriam Caldwell 4Miriam Caldwell 4
Hi Suraj, 
I'm not understanding.  If you see my code snippet I'm populating the maps with a JSON response, I'm not populating them manually...

Are you saying I can do something like this: 

map<String,Object> aretVal = new map<String,Object> {JSON.deserializeUntyped(aresp.getBody())};
      aretVal .putAll(new map<String,Object> { JSON.deserializeUntyped(bresp.getBody())});
Maharajan CMaharajan C
Hi Miriam,

your aretVal, bretVal map keys are same so it's getting replaced. 

check the key in below printed lines:
System.Debug('****** aretVal: ' + aretVal);
System.Debug('****** bretVal: ' + bretVal);

1. Overide the key with some additional unique nutmber or string in both map later you can remove it . Then use the PutAll method.
2 Otherwise add both the map in list then use it in your code for further.
List< Map<String, Object>>  abValList = new List<Map<String, Object>>();
abValList.add(aretVal);
abValList.add(bretVal);

Thanks,
Maharajan.C
Miriam Caldwell 4Miriam Caldwell 4
Hi Maharajan, 
I'm trying to go with the second option, however I'm getting an error Variable does not exist: abValList.....
Maharajan CMaharajan C
Hi,

Can you post your code here.

1. Declare the below line in top of the method. If you declared the below line inside any loop and you are trying to access it then you will get error.
List< Map<String, Object>>  abValList = new List<Map<String, Object>>();

Thanks,
Maharajan.C