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
MurrayHMurrayH 

Tests failing

I am getting the following error from one of my test scripts.

 

 Program_Session_Testing_New.runPositiveTestCases System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AddSessionMembers2: execution of BeforeInsert

caused by: System.DmlException: Insert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Leisure_Services_Client__c]: [Leisure_Services_Client__c]

 

I added the necessary Leisure Services Client data to the tables, only to have the Insert fail again, this time on row 2.  I added yet another client, only to have a failure on row 3.

 

Is there something obvious that I am doing wrong?  It's as if the system is trying to put in an extra record.

 

 

Here is the test script.

 

@isTest
private class Program_Session_Testing_New {
static testMethod void runPositiveTestCases() {
//Setup User;
//User u1 = [select id from User where alias='auser'];
//Run As U1
//System.RunAs(u1){
//List<Program_Attendance__c> addclient = new List<Program_Attendance__c>();
System.debug('Inserting user... (single record validation)');

Program__c TestProg = New Program__c (name = 'digital photos');
Insert TestProg;

Leisure_Services_Client__c TestClient = New Leisure_Services_Client__c (First_Name__c = 'Joe', Last_Name__c = 'Smith', Differentiator__c = '1', Birth_Date__c = system.today(), Postal_Code__c = 'V4V3G2');
Insert TestClient;

Leisure_Services_Client__c TestClient2 = New Leisure_Services_Client__c (First_Name__c = 'Jack', Last_Name__c = 'Smith', Differentiator__c = '1', Birth_Date__c = system.today(), Postal_Code__c = 'V4V3G2');
Insert TestClient2;


Program_Schedule__c TestSched = New Program_Schedule__c (Program__c = TestProg.id, Start_Date__c = datetime.newInstance(2009, 10, 21), Date_and_Time__c = system.now(), Normal_Credit_Hours__c = 2, Semester__c = 'Winter 2010');
Insert TestSched;

Class_Member__c classmember = new Class_Member__c(name = 'Joe Smith', Program_Schedule__c = TestSched.id, Program__c = TestProg.id, Client__c = TestClient.id);
Insert classmember;
Class_Member__c classmember2 = new Class_Member__c(name = 'Mike Smith', Program_Schedule__c = TestSched.id, Program__c = TestProg.id, Client__c = TestClient2.id);
Insert classmember2;

Program_Session__c testsession = new Program_Session__c (Program_Schedule__c = TestSched.id, Date_Time__c = datetime.newInstance(2010, 03, 01), Session_Number__c = 1, Create_Attendance_Lists__c = True);
Insert testsession;

//testsession.Credit_Hours__c = 2;

//Update testsession;


}
}

 

Here is the trigger that is failing.

 

trigger AddSessionMembers2 on Program_Session__c (Before Insert, Before Update) {

//Create list to insert into Session Attendance
List<Program_Attendance__C> ClientsforSession = New List<Program_Attendance__C>();

Set<id> ProgInstance = New Set<id>();

For (Program_Session__c PSCAC :Trigger.new){
ProgInstance.add(PSCAC.id);
ProgInstance.add(PSCAC.Program_Schedule__C);


//Check the checkboxes to see whether to proceed
If (PSCAC.Attendance_Lists_Created__c == False){
If (PSCAC.Create_Attendance_Lists__c == True) {


//Check to see if attendance is already added for the session
Double CountClientsThisSession = [Select Count() from Program_Attendance__c where Program_Session__C in :ProgInstance];
If (CountClientsThisSession == 0) {

//create a set of class members
List<Class_Member__c> AllClassMembers = New List<Class_Member__c>();
    

ID ProgSessID = PSCAC.ID;
ID ScheduleID = PSCAC.Program_Schedule__c;
String ProgSessName = PSCAC.name;

//Get the list of current attendees
List<Class_Member__c> CurrentClassMembers = New List<Class_Member__C>([Select name, Client__c from Class_Member__c where Program_Schedule__C in :ProgInstance]);

//Iterate through list
For (Class_Member__c MemberstoInsert : CurrentClassMembers){
//Check Program_Schedule_ID 

ID ClientID = MemberstoInsert.Client__c;

//build name
String ProgAttendName = ProgsessName + ' ' + MemberstoInsert.name;

ClientsforSession.add(New Program_Attendance__c(name = ProgAttendName, Leisure_Services_Client__c = ClientID, Program_Session__c = ProgSessID,Program_Schedule__c = ScheduleID));

}

}
Insert ClientsforSession;
PSCAC.Attendance_Lists_Created__c = True;
}
}
}
}


frederic baudaxfrederic baudax

Hi,

 

caused by: System.DmlException: Insert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing:[some field]:[some field]

 

Those errors are typically returned when a field is set to required in SF but not added in the object you instanciate,did you add Leisure_Services_Client__c in every object that requires it ?

 

For every object you create in your test classes always ensure required fields are in there. Alternativelly, you can also set the required flag on page layouts instead of field settings.

 

Kr,

 

Fred

 

MurrayHMurrayH

Thanks Fred:

 

As far as I can tell I added the required fields.  I even added a second record for good measure.  That's when I noticed that the error message changed.  It originally failed on the first line, but when I added another client name, it failed on the second line.  When I added yet another client name, it failed on the third line. 

 

Any ideas?

 

Murray

frederic baudaxfrederic baudax

Hi,

 

Sorry for the late reply

 

While taking a closer look at your trigger is see you create a new Program_Attendance__c, can you tell me on which object the Leisure_Services_Client__c is mandatory ? If it's on Program_Attendance__c I would try a system.debug() to check if ClientID returns correctly.

 

ClientsforSession.add(New Program_Attendance__c(name = ProgAttendName, Leisure_Services_Client__c = ClientID, Program_Session__c = ProgSessID,Program_Schedule__c = ScheduleID));
system.debug('Client Id is : ' + ClientID);