You need to sign in to do that
Don't have an account?
Miriam 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>.
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>.
Hi Miriam,
Please find the solution. You can take reference from this code.
Or
Your parent map key is root, so if you wanna add field3, you can
If it helps please mark it as Best Answer so that other people would take reference from it.
Thank You
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())});
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
I'm trying to go with the second option, however I'm getting an error Variable does not exist: abValList.....
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