Skip to main content
Post Closed as "Duplicate" by identigral, David Cheng, Rahul Gawale, Himanshu, Mohith Shrivastava apex
deleted 352 characters in body
Source Link
manb
  • 95
  • 2
  • 9

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.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;
            
            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.

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.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;
            
            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.

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.

Source Link
manb
  • 95
  • 2
  • 9

How do I take out the DML statements out of the loop?

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.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;
            
            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.