I understand that I need to use batch apex to do something like this and I have used it before as well, but not in the right way. I ended up making a list using an soql query for all the records and then, sent the records from the list 200 records at a time(due to some third party constraints) into the batch apex class. But thats probably not how its supposed to be used.
Here is my Apex class:
That first SOQL query is supposed to return over 10000 records. But I am guessing we need to cap that and send it into batch apex in an iterative way. I just not sure how to do that. Any help will be great.
public class StackEx {
public static void check(){
Map<ID,hed__Program_Enrollment__c> program_Enrollments = new Map<ID,hed__Program_Enrollment__c>([Select Id, Name, hed__Enrollment_Status__c,hed__Program_Plan__r.hed__Version__c,Program_Version__c, hed__Contact__r.Id, hed__Program_Plan__r.Name, hed__GPA__c, Non_OLMP_Final_Course__c From hed__Program_Enrollment__c Where hed__Program_Plan__r.Name IN ('Air Command and Staff College','Squadron Officer School','Air War College','Air Command and Staff College Supplemental') AND hed__Enrollment_Status__c = 'Active' ]);
Set<ID> program_Enrollment_ID_Set = new Set<ID>();
Map<hed__Program_Enrollment__c, List<hed__Course_Enrollment__c>> map_Of_PE_To_CC_List = new Map<hed__Program_Enrollment__c, List<hed__Course_Enrollment__c>>();
for(hed__Program_Enrollment__c pe: program_Enrollments.values()){
program_Enrollment_ID_Set.add(pe.Id);
}
List<hed__Course_Enrollment__c> courseConnections = [SELECT Name, hed__Program_Enrollment__r.hed__Program_Plan__r.Name,hed__Program_Enrollment__r.Name, Course_ID__c,
Id, Cloud_Course__c, hed__Status__c, hed__Course_Offering__r.hed__Course__r.Course_Type__c, hed__Program_Enrollment__r.hed__Enrollment_Status__c, Current_Seminar__c, hed__Course_Offering__r.hed__Course__r.hed__Course_ID__c, hed__Credits_Attempted__c, Common_Course__c , hed__Credits_Earned__c, hed__Program_Enrollment__r.Id, hed__Contact__r.Id FROM hed__Course_Enrollment__c WHERE hed__Program_Enrollment__r.Id IN :program_Enrollment_ID_Set AND Course_ID__c != 'GSS-502A' AND (hed__Status__c = 'Former' OR hed__Status__c = 'Current') ];
if(courseConnections.size() != 0){
for(hed__Course_Enrollment__c cc: courseConnections) {
if(program_Enrollments.containsKey(cc.hed__Program_Enrollment__r.Id)) {
if(!map_Of_PE_To_CC_List.containsKey(program_Enrollments.get(cc.hed__Program_Enrollment__r.Id))) {
map_Of_PE_To_CC_List.put(program_Enrollments.get(cc.hed__Program_Enrollment__r.Id), new List<hed__Course_Enrollment__c>());
}
map_Of_PE_To_CC_List.get(program_Enrollments.get(cc.hed__Program_Enrollment__r.Id)).add(cc);
}
}
List<hed__Program_Enrollment__c> listOfProgramEnrollmentsToUpdate = new List<hed__Program_Enrollment__c>();
for(hed__Program_Enrollment__c pe : map_Of_PE_To_CC_List.keySet()) {
Double foundationalUnits = 0.0;
Double focusedUnits = 0.0;
Integer facilitatedCourses = 0;
for(hed__Course_Enrollment__c cc: map_Of_PE_To_CC_List.get(pe)){
if(cc.Cloud_Course__c == false){
if(cc.hed__Status__c == 'Former' && cc.hed__Credits_Attempted__c == cc.hed__Credits_Earned__c){
if(cc.hed__Credits_Earned__c != null){
foundationalUnits = foundationalUnits + cc.hed__Credits_Earned__c;
}
}
else if(cc.hed__Status__c == 'Current'){
if(cc.hed__Course_Offering__r.hed__Course__r.Course_Type__c == 'Self-paced' && cc.hed__Credits_Attempted__c != null ){
foundationalUnits = foundationalUnits + cc.hed__Credits_Attempted__c;
}
else if(cc.hed__Credits_Attempted__c != null && cc.Current_Seminar__c == true){
foundationalUnits = foundationalUnits + cc.hed__Credits_Attempted__c;
system.debug('Current focused Course name: '+ cc.Name);
}
}
}
else if(cc.Cloud_Course__c == true){
if(cc.hed__Course_Offering__r.hed__Course__r.Course_Type__c == 'facilitated'){
facilitatedCourses = facilitatedCourses + 1;
system.debug('Focused Facilitated Course name: '+ cc.Name);
}
if(cc.hed__Status__c == 'Former' && cc.hed__Credits_Attempted__c == cc.hed__Credits_Earned__c){
if(cc.hed__Credits_Earned__c != null){
focusedUnits = focusedUnits + cc.hed__Credits_Earned__c;
system.debug('Former focused Course name: '+ cc.Name);
}
}
else if(cc.hed__Status__c == 'Current'){
if(cc.hed__Course_Offering__r.hed__Course__r.Course_Type__c == 'Self-paced' && cc.hed__Credits_Attempted__c != null ){
focusedUnits = focusedUnits + cc.hed__Credits_Attempted__c;
}
else if(cc.hed__Credits_Attempted__c != null && cc.Current_Seminar__c == true){
focusedUnits = focusedUnits + cc.hed__Credits_Attempted__c;
system.debug('Current focused Course name: '+ cc.Name);
}
}
}
}
System.debug('PROGRAM ENROLLMENT: '+ pe.Name);
System.debug('PROGRAM Plan: '+ pe.hed__Program_Plan__r.Name);
System.debug('FoundationalUnits: '+ foundationalUnits);
System.debug('FocusedUnits: '+ focusedUnits);
System.debug('Facilitated: '+ facilitatedCourses);
if(pe.hed__Program_Plan__r.Name == 'Air Command and Staff College' && foundationalUnits == 43.0 && focusedUnits >= 9.0 && facilitatedCourses == 1 ){
pe.Non_OLMP_Final_Course__c = true;
listOfProgramEnrollmentsToUpdate.add(pe);
}
else if(pe.hed__Program_Plan__r.Name == 'Air War College' ) {
if(pe.Program_Version__c == '18' && foundationalUnits == 30.0 && focusedUnits >= 9.0 && facilitatedCourses == 1){
pe.Non_OLMP_Final_Course__c = true;
listOfProgramEnrollmentsToUpdate.add(pe);
} else if(pe.Program_Version__c == '19' && foundationalUnits == 31.0 && focusedUnits >= 9.0 && facilitatedCourses == 1){
pe.Non_OLMP_Final_Course__c = true;
listOfProgramEnrollmentsToUpdate.add(pe);
}
}
else if(pe.hed__Program_Plan__r.Name == 'Air Command and Staff College Supplemental' && foundationalUnits == 31) {
pe.Non_OLMP_Final_Course__c = true;
listOfProgramEnrollmentsToUpdate.add(pe);
}
else if(pe.hed__Program_Plan__r.Name == 'Squadron Officer School' && foundationalUnits == 27.0 && focusedUnits >= 9.0 && facilitatedCourses == 1 ) {
pe.Non_OLMP_Final_Course__c = true;
listOfProgramEnrollmentsToUpdate.add(pe);
}
}
if(listOfProgramEnrollmentsToUpdate.size() != 0){
System.debug('ListOfPE to Update: '+ listOfProgramEnrollmentsToUpdate );
Update listOfProgramEnrollmentsToUpdate;
}
}
}
}