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
sfdc development hintssfdc development hints 

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

Best Answer chosen by Admin (Salesforce Developers) 
tom_patrostom_patros

Here's a basic test class:

 

@isTest 
public class EnrollCandidateTests {

    static testMethod void test() {
       
// create an enroll candidate
// populate with whatever fields you need to support your trigger
Enroll_Candidate__c ec = new Enroll_Candidate__c(Name = 'TEST');
// insert the record insert ec; } }

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

tom_patrostom_patros

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.

sfdc development hintssfdc development hints

yeah i need more clarification on this testclass for this  trigger , because i am new in writing testclasses for triggers.

sfdc development hintssfdc development hints

//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();
}

tom_patrostom_patros

Here's a basic test class:

 

@isTest 
public class EnrollCandidateTests {

    static testMethod void test() {
       
// create an enroll candidate
// populate with whatever fields you need to support your trigger
Enroll_Candidate__c ec = new Enroll_Candidate__c(Name = 'TEST');
// insert the record insert ec; } }

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.

This was selected as the best answer
sfdc development hintssfdc development hints

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

tom_patrostom_patros

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).