+ Start a Discussion
Katie KourtakisKatie Kourtakis 

Need Help Using Ids to create new Records

Hello community, I am attempting to write an apex class that is scheduled to run weekly. The three objects I am dealing with is 'Program Enrollment',  'Volunteer Shift' and 'Attendance'. The Program Enrollment object has a lookup to the campaign, contact, and volunteer job objects. The Attendance object has a lookup to the program enrollment and master-detail to Volunteer shift. I am attempting to create attendance records for each program enrollment I have for the volunteer shifts in the next seven days.
The volunteer shifts are created in advanced. 

Here is my code so far:
global class createAttendance implements Schedulable {
    global void execute(SchedulableContext ctx) {
        //Find all attending Participants with Status Confirmed
        Set<Id> enroll = new map<Id, Program_Enrollment__c>([SELECT ID FROM Program_Enrollment__c  WHERE Campaign__c = '7016A000000dWoDQAU' AND Status__c = 'Confirmed']).keySet();
        //Find Volunteer Shifts >= Today and < Today + 7
  Set<Id> shift = new Map<Id, GW_Volunteers__Volunteer_Shift__c>([SELECT ID FROM GW_Volunteers__Volunteer_Shift__C WHERE Program__c = '7016A000000dWoDQAU' AND Shift_Start_Date__c >= TODAY AND Shift_Start_Date__c = NEXT_n_DAYS:7]).keySet();

        //Create Attendance for each program enrollment
        //Loop through enrollments
        for(Integer i = 0; i < enroll.size(); i++) {
            //Loop through Volunteer Shifts
            for(Integer j = 0; j < shift.size(); j++) {
                //Create attendance for each enrollment
                Attendance__c att = new Attendance__c();
                insert att;

In this code block 
Attendance__c att = new Attendance__c();
I need to set the Program enrollment lookup field to the Id I obtained using SOQL. Same for the Volunteer Shift field. I just haven't come across anything to show me how.

Help would be appreciated. Thanks!