1

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.

0

1 Answer 1

1

Make a new map to store the parent records:

Map<hed__Course_Offering__c, hed__Course_Offering__c> courseOfferings = new Map<hed__Course_Offering__c, hed__Course_Offering__c>();
for(hed__Course_Offering__c key: cOMap.keySet()) {  
     hed__Course_Offering__c newCO = new hed__Course_Offering__c();
     // ... omitted ... //
     courseOfferings.put(key, newCO);
}

Then insert the records:

insert courseOfferings.values();

Then gather your updates:

hed__Course_Enrollment__c[] enrollments = new hed__Course_Enrollment__c[0];
for(hed__Course_Offering__c key: cOMap.keySet()) {  
    for(hed__Course_Enrollment__c value: cOMap.get(key) ){
        hed__Course_Enrollment__c ccs = value;
        ccs.hed__Course_Offering__c = courseOfferings.get(key).Id;
        enrollments.add(ccs);
    }
}
update entrollments;

Then update the records:

update updates;

Full Listing:

Map<hed__Course_Offering__c, hed__Course_Offering__c> courseOfferings = new Map<hed__Course_Offering__c, hed__Course_Offering__c>();
for(hed__Course_Offering__c key: cOMap.keySet()) {  
    hed__Course_Offering__c newCO = new hed__Course_Offering__c();
    newCO.hed__Course__c = CO.hed__Course__c;
    newCO.hed__Term__c = CO.hed__Term__c;
    newCO.Name = CO.Name;
    newCO.Status__c = 'Active'; 
    newCO.At_Capacity__c = CO.At_Capacity__c;
    newCO.Blueprint_SIS_ID__c = CO.Blueprint_SIS_ID__c;
    newCO.hed__Capacity__c = CO.hed__Capacity__c;
    newCO.Course_Instructor__c = CO.Course_Instructor__c;
    courseOfferings.put(key, newCO);
}
insert courseOfferings.values();
hed__Course_Enrollment__c[] enrollments = new hed__Course_Enrollment__c[0];
for(hed__Course_Offering__c key: cOMap.keySet()) {  
    for(hed__Course_Enrollment__c value: cOMap.get(key) ){
        hed__Course_Enrollment__c ccs = value;
        ccs.hed__Course_Offering__c = courseOfferings.get(key).Id;
        enrollments.add(ccs);
    }
}
update entrollments;
10
  • So just to clarify, the new course offerings are being made from one existing course offering which have the related course enrollments. Then those course enrollments are being divided into the new created course offerings from that one course offering. What I am not able to understand is that, the Id's for the newly created course offerings will be created on insert, right ? But you are using the keys to associate the course enrollments. Which isnt making sense, because those keys havent recieved their ID's yet since the havent been inserted. What you have inserted is the values.
    – manb
    Sep 10, 2020 at 14:46
  • I was still editing the above. please can you check @sfdcfox
    – manb
    Sep 10, 2020 at 14:48
  • @manb The keys should not be changed. We're creating new records in memory, then inserting those; they are still associated to the original keys, which we then use to get the enrollments.
    – sfdcfox
    Sep 10, 2020 at 14:53
  • Trying to make sense of it and testing it out right now. Will get back to you. Thank you :D
    – manb
    Sep 10, 2020 at 14:55
  • so do you mean, the ID which is generated on inserting the values associated with the keys will be the same on the keys as well? And when I have to associate the enrollments to those key ID's?
    – manb
    Sep 11, 2020 at 12:59

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