You need to sign in to do that
Don't have an account?

Hi , could you please help me with the test class for the trigger below
trigger enrollcandidatetrigger on Enroll_Candidate__c(after delete, after insert, after update,after undelete)
{
Set<ID> enrollIds= new Set<ID>();
Boolean setError=false;
Enroll_Candidate__c[] coursedetails;
if(Trigger.IsDelete)
coursedetails=Trigger.old;
else
coursedetails=Trigger.new;
Set<ID> CourseId= new set<ID>();
for(Enroll_Candidate__c candidates:coursedetails)
{
CourseId.add(candidates.Course_Details__c);
}
Map<ID,Course_Details__c> coursedetailsforcandidates=new Map<ID,Course_Details__c>([Select Id, Booked_Slots__c, Total_Slots__c, (Select Id from Enroll_Candidates__r ) from Course_Details__c where Id in:CourseId]);
for(Enroll_Candidate__c candidates:[Select Id , Course_Details__c FROM Enroll_Candidate__c where Course_Details__c in:coursedetailsforcandidates.keyset()])
enrollIds.add(candidates.ID);
for(Course_Details__c courses :coursedetailsforcandidates.values())
{
System.debug('The total seats in the map is :' + courses.Total_Slots__c + ' the ids populated is : ' + enrollIds.size());
if(courses.Total_Slots__c >= enrollIds.size())
courses.Booked_Slots__c=enrollIds.size();
else
setError = true;
}
if(setError)
Trigger.new[0].addError('Booked seats should not be the same as the Total seats!!');
update coursedetailsforcandidates.values();
}
Here's a basic test class:
Test classes are just classes that hold a bunch of methods marked as "testMethod". Those methods are responsible for executing actions that will touch (aka "cover") your other code. In this case, the test() method in EnrollCandidateTests inserts an Enroll_Candidate__c record will fire your trigger you previously posted.
The whole class is marked with "@isTest" at the top to indicate to the platform that this class only contains testMethods and, therefore, this code does _not_ require test coverage.
All Answers
Basically, you need a test class that creates an Enroll_Candidate__c object. Maybe call it "TestEnrollCandidate" - make sure you insert the record with fields relevant for your trigger.
Let us know if you need more help.
yeah i need more clarification on this testclass for this trigger , because i am new in writing testclasses for triggers.
//Trigger on "enrollpeople" object which allows the field "Booked slots" to be incremented as the "EnrollPeople" is added and also verifying that "Booked slots" should not be greater than "Totalslots" //
trigger enrollcandidatetrigger on Enroll_Candidate__c(after delete, after insert, after update,after undelete)
{
Set<ID> enrollIds= new Set<ID>();
Boolean setError=false;
Enroll_Candidate__c[] coursedetails;
if(Trigger.IsDelete)
coursedetails=Trigger.old;
else
coursedetails=Trigger.new;
Set<ID> CourseId= new set<ID>();
for(Enroll_Candidate__c candidates:coursedetails)
{
CourseId.add(candidates.Course_Details__c);
}
Map<ID,Course_Details__c> coursedetailsforcandidates=new Map<ID,Course_Details__c>([Select Id, Booked_Slots__c, Total_Slots__c, (Select Id from Enroll_Candidates__r ) from Course_Details__c where Id in:CourseId]);
for(Enroll_Candidate__c candidates:[Select Id , Course_Details__c FROM Enroll_Candidate__c where Course_Details__c in:coursedetailsforcandidates.keyset()])
enrollIds.add(candidates.ID);
for(Course_Details__c courses :coursedetailsforcandidates.values())
{
System.debug('The total seats in the map is :' + courses.Total_Slots__c + ' the ids populated is : ' + enrollIds.size());
if(courses.Total_Slots__c >= enrollIds.size())
courses.Booked_Slots__c=enrollIds.size();
else
setError = true;
}
if(setError)
Trigger.new[0].addError('Booked seats should not exceed Total seats');
update coursedetailsforcandidates.values();
}
Here's a basic test class:
Test classes are just classes that hold a bunch of methods marked as "testMethod". Those methods are responsible for executing actions that will touch (aka "cover") your other code. In this case, the test() method in EnrollCandidateTests inserts an Enroll_Candidate__c record will fire your trigger you previously posted.
The whole class is marked with "@isTest" at the top to indicate to the platform that this class only contains testMethods and, therefore, this code does _not_ require test coverage.
i am getting the below error when i run the test class
Error: Compile Error: Field is not writeable: Enroll_Candidate__c.Name at line 6 column 54
what does it indicate and how do i fix it
I'm guessing the Name field on Enroll_Candidate__c is an autonumber and therefore is read-only. Just remove the line that sets the name and you should be good (unless you have other required fields on your object).