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();
}
}
Best Answer
When you have a
Map<T1, T2>
and call values, you get aList<T2>
. So when you have aMap<T, List<T>>
, you get aList<List<T>>
. Basically, you're going to have to loop through if you want to flatten your map: