• MurrayH
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies

I have complete test success with 86% code coverage in the sandbox, but they fail in deployment.  See my post below entitled "tests failing". Any ideas anyone?

 

 

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


I am having problems with the governor limits on SOQL queries.  I am trying to populate a list.  I tried the "For" procedure as outlined in the Apex Documentation, but I couldn't get it to work.  I also tried to break the query into three parts and then add the parts to the list, and that didn't work either.  I am new to this, so any comments would be really appreciated.

 

 

trigger ClassList1 on Program_Schedule__c (after insert, after update) {

//create a variable to hold the records to insert into the Class Members Object

List<Class_Member__c> AddClassMembers = New List<Class_Member__c>();

//Get a list of approved clients for all programs

 

// using addall to list methodology

//List<Program_Interest__c> ApprovedClients = New List<Program_Interest__C>();
//ApprovedClients.addall ([Select Client__c, Already_Enrolled__c, Program_of_Interest__c from Program_Interest__c where Name < 'G' and Next_Session__c = true AND Already_Enrolled__c = False]);
//ApprovedClients.addall ([Select Client__c, Already_Enrolled__c, Program_of_Interest__c from Program_Interest__c where Name >= 'H' and Name < 'I' and Next_Session__c = true AND Already_Enrolled__c = False]);
//ApprovedClients.addall ([Select Client__c, Already_Enrolled__c, Program_of_Interest__c from Program_Interest__c where Name >= 'T' and Next_Session__c = true AND Already_Enrolled__c = False]);

 

//Using the For methodology
For(List<Program_Interest__c> ApprovedClients : [Select Client__c, Already_Enrolled__c, Program_of_Interest__c from Program_Interest__c where Next_Session__c = true AND Already_Enrolled__c = False])
{
//Get existing class lists for duplicate checking
List<Class_Member__c> ExistingClassMembers = New List<Class_Member__C>([Select Program_Schedule__C from Class_Member__C]);

//Get list size
Double TotalNumApprovedClients = ApprovedClients.Size();

//Create a trigger list to hold ids
List<id> ProgInstance = New List<id>();

//Iterate through trigger records
For (Program_Schedule__c PSACM : trigger.new){
//PSACM.Class_List_Created__C = True;
//PSACM.Create_Class_List__C = False;
ProgInstance.add(PSACM.id);
ProgInstance.add(PSACM.Program__c);
ID ProgSchedID = PSACM.ID;
ID ProgID = PSACM.Program__c;

//Check to see if create box is checked
If (PSACM.Create_Class_List__C == true){


//Double CountClients = [Select Count() from Program_Interest__c where Next_Session__c = true AND Id in :ProgInstance ];

//iterate through list of approved clients and add those with matching program id's to the AddClassMembers list
For (Program_Interest__c APPCL : ApprovedClients)
{
If (APPCL.Program_of_Interest__c == ProgID)
{
ID ClientID = APPCL.Client__c;
APPCL.Already_Enrolled__c = True;

AddClassMembers.add(New Class_Member__c(Client__c = ClientID, Program_Schedule__c = ProgSchedID, Program__c = ProgID ));

}
}

Double CountClientstobeadded = AddClassMembers.size();

//Check to see if the number of clients is greater than the class capacity
Double ClassCap = PSACM.Capacity__c;

If (ClassCap < CountClientstobeadded){
PSACM.AddError ('Too many clients for the class, adjust client numbers or increase class size');}

//check to see if class list exists
Double CountExistingClients = [Select Count() from Class_Member__c where Program_Schedule__C in :ProgInstance ];
Double ClassPlacesLeft = ClassCap - CountExistingClients;

If (CountClientstobeAdded  > ClassPlacesLeft){
PSACM.AddError ('Clients previously added, check and add manually if necessary');
}
//If (CountExistingClients == 0){


//If (ClassCap >= CountClientstobeadded){

//For (Program_Schedule__c PSACM : trigger.new)
//PSACM.Class_List_Created__C = True;
//PSACM.Create_Class_List__C = False;

try{
insert AddClassMembers;
Update ApprovedClients;
//Update PSACM;
}
catch (DmlException de) {
for (Integer i = 0; I < de.getNumDml(); i++) {
PSACM.addError(de.getDmlMessage(i));

}
}
}}
}}

I have complete test success with 86% code coverage in the sandbox, but they fail in deployment.  See my post below entitled "tests failing". Any ideas anyone?

 

 

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


I am having problems with the governor limits on SOQL queries.  I am trying to populate a list.  I tried the "For" procedure as outlined in the Apex Documentation, but I couldn't get it to work.  I also tried to break the query into three parts and then add the parts to the list, and that didn't work either.  I am new to this, so any comments would be really appreciated.

 

 

trigger ClassList1 on Program_Schedule__c (after insert, after update) {

//create a variable to hold the records to insert into the Class Members Object

List<Class_Member__c> AddClassMembers = New List<Class_Member__c>();

//Get a list of approved clients for all programs

 

// using addall to list methodology

//List<Program_Interest__c> ApprovedClients = New List<Program_Interest__C>();
//ApprovedClients.addall ([Select Client__c, Already_Enrolled__c, Program_of_Interest__c from Program_Interest__c where Name < 'G' and Next_Session__c = true AND Already_Enrolled__c = False]);
//ApprovedClients.addall ([Select Client__c, Already_Enrolled__c, Program_of_Interest__c from Program_Interest__c where Name >= 'H' and Name < 'I' and Next_Session__c = true AND Already_Enrolled__c = False]);
//ApprovedClients.addall ([Select Client__c, Already_Enrolled__c, Program_of_Interest__c from Program_Interest__c where Name >= 'T' and Next_Session__c = true AND Already_Enrolled__c = False]);

 

//Using the For methodology
For(List<Program_Interest__c> ApprovedClients : [Select Client__c, Already_Enrolled__c, Program_of_Interest__c from Program_Interest__c where Next_Session__c = true AND Already_Enrolled__c = False])
{
//Get existing class lists for duplicate checking
List<Class_Member__c> ExistingClassMembers = New List<Class_Member__C>([Select Program_Schedule__C from Class_Member__C]);

//Get list size
Double TotalNumApprovedClients = ApprovedClients.Size();

//Create a trigger list to hold ids
List<id> ProgInstance = New List<id>();

//Iterate through trigger records
For (Program_Schedule__c PSACM : trigger.new){
//PSACM.Class_List_Created__C = True;
//PSACM.Create_Class_List__C = False;
ProgInstance.add(PSACM.id);
ProgInstance.add(PSACM.Program__c);
ID ProgSchedID = PSACM.ID;
ID ProgID = PSACM.Program__c;

//Check to see if create box is checked
If (PSACM.Create_Class_List__C == true){


//Double CountClients = [Select Count() from Program_Interest__c where Next_Session__c = true AND Id in :ProgInstance ];

//iterate through list of approved clients and add those with matching program id's to the AddClassMembers list
For (Program_Interest__c APPCL : ApprovedClients)
{
If (APPCL.Program_of_Interest__c == ProgID)
{
ID ClientID = APPCL.Client__c;
APPCL.Already_Enrolled__c = True;

AddClassMembers.add(New Class_Member__c(Client__c = ClientID, Program_Schedule__c = ProgSchedID, Program__c = ProgID ));

}
}

Double CountClientstobeadded = AddClassMembers.size();

//Check to see if the number of clients is greater than the class capacity
Double ClassCap = PSACM.Capacity__c;

If (ClassCap < CountClientstobeadded){
PSACM.AddError ('Too many clients for the class, adjust client numbers or increase class size');}

//check to see if class list exists
Double CountExistingClients = [Select Count() from Class_Member__c where Program_Schedule__C in :ProgInstance ];
Double ClassPlacesLeft = ClassCap - CountExistingClients;

If (CountClientstobeAdded  > ClassPlacesLeft){
PSACM.AddError ('Clients previously added, check and add manually if necessary');
}
//If (CountExistingClients == 0){


//If (ClassCap >= CountClientstobeadded){

//For (Program_Schedule__c PSACM : trigger.new)
//PSACM.Class_List_Created__C = True;
//PSACM.Create_Class_List__C = False;

try{
insert AddClassMembers;
Update ApprovedClients;
//Update PSACM;
}
catch (DmlException de) {
for (Integer i = 0; I < de.getNumDml(); i++) {
PSACM.addError(de.getDmlMessage(i));

}
}
}}
}}