4

How do I insert the values from the map? I want to insert the grandchildren records in variable Commlinesmap, but I get this error:

DML requires SObject or SObject list type: List>

Error is at line: insert commlinesmap.values();

public class QuotaClone {

    public static void clonequota (Quota_Stat__c  qstat) 
    {
        Map<Rep_comm__c,List<Commission_line__c>> Commlinesmap= new Map<Rep_comm__c,List<Commission_line__c>>();
        List<Rep_comm__c> newRepList = new List<Rep_comm__c>();

        //Fetch the child (Rep_comm__c) and grandchild (commission_line__c) from parent record qstat
        List<Rep_comm__c> RepList = [Select r.Vendor__c, r.Name, r.Id, (Select Name, Start_Date__c, End_Date__c, Rep__c From Commisions__r) From Rep_Comm__c r where vendor__c = :qstat.Id];

        Quota_Stat__c newqstat = qstat.clone(false, true); //do a deep clone of parent

        //insert the parent (quota stat) record
        insert newqstat;

        // clone the child records (Rep_comm__c) 
        for(Rep_comm__c rcom : RepList){
            Rep_comm__c newRepcomm = rcom.clone(false, true); //do a deep clone
            newrepcomm.vendor__c = newqstat.Id;
            newRepList.add(newRepcomm);
            //add the cloned grand child records (Commission_line__c) to map
            Commlinesmap.put(newRepcomm,rcom.Commisions__r.deepClone(false,false,false));
        }
       // insert child records (Rep_comm__c)
            insert newRepList;
          //match the grandchild records to child records
        for(Rep_comm__c rcomm :Commlinesmap.keySet()){
            for(Commission_line__c comline : Commlinesmap.get(rcomm)){
                comline.Rep__c = rcomm.Id;
            }
        }

                 //**not sure how to insert these values....**
                    insert Commlinesmap.values();

    } 
}

1 Answer 1

8

When you have a Map<T1, T2> and call values, you get a List<T2>. So when you have a Map<T, List<T>>, you get a List<List<T>>. Basically, you're going to have to loop through if you want to flatten your map:

List<Commission_line__c> records = new List<Commission_line__c>();
for (List<Commission_line__c> grouping : myMap.values())
    records.addAll(grouping);
insert records;
5
  • 2
    +1 Or flatten it at the same time as building the map, since there's already a loop building the map.
    – sfdcfox
    May 10, 2017 at 20:33
  • How do I do this?
    – Sammy
    May 10, 2017 at 20:34
  • @sfdcfox I just looked at the type declaration and the insert statement. My eyes are glazing over a little when I try to read the rest. Maybe I will have fresher eyes later.
    – Adrian Larson
    May 10, 2017 at 20:36
  • @Adrian, can you please explain this line: for (List<Commission_line__c> grouping : myMap.values()) records.addAll(grouping);
    – Sammy
    May 10, 2017 at 20:38
  • @Sammy7 You're looping through all of the grouped lists and combining them into one so that you can operate on it.
    – Adrian Larson
    May 10, 2017 at 20:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .