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

insert key of map as parent objects and value of map as child objects

I need to insert key of map as parent objects and value of map as child object. 
Map<Case,List<Case>> parentChildMap = new Map<Case,List<Case>>();

How can we do this?
Prateek Prasoon 25Prateek Prasoon 25
List<Contact> contactList=[select id,accountid from Contact where accountid!=null];
        map<Id,List<Contact>> contactMap=new map<Id,List<Contact>>();
        for(Contact con:contactList){
        list<Contact>conList = new list<Contact>();
                conList = contactMap.get(con.accountid)
            contactMap.put(con.accountid, conList);

If you find this answer helpful, Please mark it as the best answer.
Arun Kumar 1141Arun Kumar 1141
Hello Pc,

You can try below code,

public class InsertCaseMap {
    public static void methForCase(){
       Map<Case,list<Case>> caseslist=new Map<Case,list<Case>>();
       list<Case> caselist=[SELECT id,(SELECT id from Cases) FROM Case];
        for(Case cs:caselist){

Hope this will help you.