function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Katie KourtakisKatie Kourtakis 

Required_Field_Missing GW_Volunteers__Volunter_Job__c

Hello, I built a test class and ran it receiving this error message: Required_Field_Missing on line 41 Column 1. GW_Volunteers__Volunter_Job__c

I created a new volunteer job, retrieved the Id of the volunteer job, and set the volunteer job field to the id when creating a new Volunteer Shift. I highlighted the sections in bold.

@isTest
public class createSoulAttendanceTest {

    public static String CRON_EXP = '0 0 0 3 7 ? 2019';
    static testmethod void testScheduledJob() {
        
        //Create Contact
        Contact con = new Contact(Lastname = 'Brown');
        
        insert con;
        
        //Grab contact Id
        Id conId = con.Id;
        
        //Create Campaign
        Campaign soulCam = new Campaign(Name = 'Soul Studio');
        
        insert soulCam;
        
        //Grab Campaign Id
        Id camId = soulCam.Id;
        
        //Create Volunteer Job
        GW_Volunteers__Volunteer_Job__c job = new GW_Volunteers__Volunteer_Job__c(GW_Volunteers__Campaign__c = camID, Name = 'Monday');
        //Grab Volunteer Job ID
        Id jobId = job.Id;

        
        //Set Date/Time for Volunteer Shifts
        DateTime dt = DateTime.now(); 
        
        //Create list to hold all the shifts
        List<Id> allShiftList = new List<Id>();
        
        //Create 7 Volunteer Shifts
        for(Integer i = 0; i < 6; i++) {
            GW_Volunteers__Volunteer_Shift__c shift = new GW_Volunteers__Volunteer_Shift__c(Program__c = camID,                 GW_Volunteers__Volunteer_Job__c = jobId,                                         GW_Volunteers__Start_Date_Time__c = dt.addDays(i),                                                               GW_Volunteers__Duration__c = 5);
            insert shift;
            
            allShiftList.add(shift.Id);
            
        }
        
        //Create New Program Enrollment
        Program_Enrollment__c proEnroll = new Program_Enrollment__c(Campaign__c = camId,
                                                                   Attendee__c = conId,
                                                                   Volunteer_Job__c = jobId,
                                                                   Status__c = 'Confirmed',
                                                                   Role__c = 'Participant',
                                                                   For_All_Sessions__c = FALSE);
        //Retrieve Program Enrollment Id
        Id proId = proEnroll.Id;
        
       Test.startTest();
       String apexJobId = System.schedule('ScheduleApexTest', CRON_EXP, new createSoulAttendance());
       Test.stopTest();
        
       List<Attendance__c> att = [SELECT Id FROM Attendance__c
                                 WHERE Program_Enrollment__c =: proId
                                 AND Date__c >= TODAY 
                                 AND Date__c = NEXT_n_DAYS:7];
        
       System.assertEquals(allShiftList.size(), att.size());
    }
}

Any help in why this error is happening is greatly appreciated!
Best Answer chosen by Katie Kourtakis
Vikash GoyalVikash Goyal
Hi Katie,

You have missed to insert job record :

//Create Volunteer Job
GW_Volunteers__Volunteer_Job__c job = new GW_Volunteers__Volunteer_Job__c(GW_Volunteers__Campaign__c = camID, Name = 'Monday');
insert job;
 //Grab Volunteer Job ID
  Id jobId = job.Id;


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks

All Answers

Vikash GoyalVikash Goyal
Hi Katie,

You have missed to insert job record :

//Create Volunteer Job
GW_Volunteers__Volunteer_Job__c job = new GW_Volunteers__Volunteer_Job__c(GW_Volunteers__Campaign__c = camID, Name = 'Monday');
insert job;
 //Grab Volunteer Job ID
  Id jobId = job.Id;


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks
This was selected as the best answer
Katie KourtakisKatie Kourtakis
Thanks Vikash! I also noticed I forgot to add an insert after creating the program enrollment object.