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
PCPC 

The values of Map id getting null after I perform Insert operation on the key set.

I am trying to insert the a Map of <Case,List<Case> as parents and their related child cases using apex. But once I insert the Map key set , the map values get null.
Map<Case,List<Case>> parentChildMap = new Map<Case,List<Case>>();
 List<Case> parentCases = new List<Case>();
        for(CaseWrapper wrap: caseWrapperList){
            parentChildMap.put(wrap.parentCase,wrap.childCases);
        }
        System.debug('map before insert==>'+parentChildMap);
        if(parentChildMap.keySet() !=null){
           parentCases.addAll(parentChildMap.keySet());
           insert parentCases ; 
           System.debug('map after insert==>'+parentChildMap);
        }


Debugs:
map before insert==> {Case:{Subject=1}=(Case:{Subject=1.1}), Case:{Subject=2}=(Case:{Subject=2.2})}

map after insert==> {Case:{Subject=1, Id=5000K00002cxQsWQAU}=null, Case:{Subject=2, Id=5000K00002cxQsXQAU}=null}

Why the values are geting set to null?
Chris JericoChris Jerico


Please check this link to see if it helps

https://salesforce.stackexchange.com/questions/296368/sobject-key-of-map-mutated-returns-null-but-serializing-returns-the-value

PCPC
Hi Chris,
Thanks for the link. Yes if I debug Json.serialize(parentChildMap) then I am getting the map values as well. But can you help me with how can i use this serialized data to loop over something like:
if(parentChildMap.keySet() !=null){
           parentCases.addAll(parentChildMap.keySet());
           insert parentCases ; 
           System.debug('map key set==>'+parentChildMap.keySet());
           System.debug('map 2==>'+JSON.deserialize(parentChildMap));
        }
        for(Case parentCase : parentChildMap.keySet()){
           System.debug('child case list==>'+parentChildMap.get(parentCase));
            for(Case childCase: parentChildMap.get(parentCase)){
                childCase.ParentId=parentCase.Id;
                childCasesToUpdate.add(childCase);
            } 
        }
        update childCasesToUpdate;

 
Chris JericoChris Jerico

I think the following might be the solution to your problem,
I mean can you concatenate some field values in the inserted data into a unique value before and after the data is inserted
Map<String,List<Case>> parentChildMap = new Map<String,List<Case>>();
// Step 1
for(CaseWrapper wrap: caseWrapperList){
  String uniqueKey = XX + XX + XX + XX;
  parentChildMap.put(uniqueKey,wrap.childCases);
  parentNewCases.add(wrap.parentCase);
}
insert parentNewCases;

// Step 2
List<Case> updSubCaseList = new List<Case>();
for (Case item : parentNewCases) {
   String uniqueKey= item.XX + item.XX + item.XX + item.XX;
   if (parentChildMap.get(uniqueKey) != null) {
     for (Case subItem : parentChildMap.get(unqiueKey)) {
        subItem.parentId = item.Id;
        updSubCaseList.add(subItem);
     }
   }
}
update updSubCaseList;