I am new to apex, I've achieved the basic working code of what I had to, in java. But now I am told that I cant do DML statements or queries inside loops. Any help/explanation as to how I could I do this will be appreciated.
for(hed__Course_Offering__c key: cOMap.keySet()) {
hed__Course_Offering__c newCO = new hed__Course_Offering__c();
newCO.Name = CO.Name;
newCO.Status__c = 'Active';
insert newCO;
for(hed__Course_Enrollment__c value: cOMap.get(key) ){
hed__Course_Enrollment__c ccs = value;
ccs.hed__Course_Offering__c = newCO.Id;
update ccs;
System.debug('Key: ' + key + ' Value: ' + value);
}
}
I figured out how to take the update method out of the loop, but I am not able to understand how to take out the insert method, as the update method is based on the newly inserted records ID.