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
Gary Payne 9Gary Payne 9 

Need a sample Test Class for Case Trigger that updates the number of related cases

I have a Case Trigger that updates a custom number field, Number_of_Related_Cases__c., that I obtained from the Success Community.  Following is the Trigger code:
trigger Case_Count_Related_Cases on Case (after insert, after update, after delete) {

//build list of parent case ids
    Set<Id> ParentIds = new Set<Id>();

  //If insert or update, use trigger.new
  if (trigger.isinsert || trigger.isupdate) {

  //if case has parent, add to set of parentids
  for (Case c: Trigger.New) {

      if(c.ParentId != null) {
      ParentIds.add(c.ParentId);
      }

  //if case previously had parent, but no longer does, also add so we can recalc
  if (trigger.isupdate) {

      if (trigger.oldmap.get(c.id).parentid != null && c.ParentId == null) {
      ParentIds.add(trigger.oldmap.get(c.id).parentid);
      }

     }

  }
  }

  //if delete, make sure we update parent
  else if (trigger.isdelete) {

  for (Case c: Trigger.Old) {

      if(c.ParentId != null) {
      ParentIds.add(c.ParentId);
      }

  }
  }

  //then query for parent cases, and do inner query for related cases
    List<Case> pcount = [Select Id, (Select Id from Cases) From CASE where Id IN :ParentIds];

              //use size of inner cases to set number count
              for (Case pc: pcount) {
              pc.Number_of_Related_Cases__c = pc.cases.size();
              }
     //update parents        
    update pcount;
}

I am not a developer and need to create a Test Class for this trigger.  What would a Test Class for this Trigger look like?
Best Answer chosen by Gary Payne 9
Ajay K DubediAjay K Dubedi
Hi Gary,
Test class are very simple you just have to create the dummy records of the object which are been used the your class or trigger.
Simple Example of trigger and test class for that trigger.
Trigger on Contact
trigger RestrictContactByName on Contact (before insert, before update){
/check contacts prior to insert or update for invalid data 
For(Contact c : Trigger.New){ 
if(c.LastName == 'INVALIDNAME'){ 
//invalidname is invalid  
c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');                
    }      
  } 
​}
Test Class(Apex class):
@isTest 
public class TestRestrictContactByName{ 
@isTest 
static void testRestrictContactbyName(){ 
test.startTest(); 
Contact con=new Contact(); 
con.FirstName='bccccc'; 
con.LastName='INVALIDNAME'; 
Insert con;
list<Contact> conlist=new list<Contact>{con}; 
Test.StopTest();
   } ​
​ }
By looking at the above example , create a text class and For your test class you have to follow the following steps:
 
Create a method Fisrt create a Case record..
//this will be the parent Case 
Case Parentcase = new Case(); 
  Parentcase.Status ='Working'; 
  Parentcase.Origin = 'Email'; 
  Parentcase.Number_of_Related_Cases__c = 0; 
Insert Parentcase; 
// Creating a Child Case 
Case Childcase = new Case(); 
  Childcase.Status = 'Working'; 
  Childcase.Origin = 'Email'; 
  Childcase.ParentId = Parentcase.Id; 
Insert Childcase; 
/* From above code the insert after insert event will be fired.*/ 
  Childcase.ParentId = Null; 
Update Childcase; 
/*From the above code your update event will be fired.*/ 
Make second method for the delete event
//this will be the parent Case 
Case Parentcase = new Case(); 
  Parentcase.Status ='Working'; 
  Parentcase.Origin = 'Email'; 
  Parentcase.Number_of_Related_Cases__c = 0;
Insert Parentcase;
//Child Case 
Case Childcase = new Case(); 
   Childcase.Status ='Working'; 
   Childcase.Origin = 'Email'; 
   Childcase.ParentId = Parentcase.Id;
 Insert Childcase; Delete Childcase;
/*From above code your delete event will be fired */
As you will write the code you should write test.startTest() at the beginning and Test.stopTest() at the end as shown in the above example.
This comes under best practices of writing test classes and for further knowledge you can go to the below link:-
https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_testing  

Regards
Ajay,
Please mark my answer as a solution if it is helpful.

All Answers

Ajay K DubediAjay K Dubedi
Hi Gary,
Test class are very simple you just have to create the dummy records of the object which are been used the your class or trigger.
Simple Example of trigger and test class for that trigger.
Trigger on Contact
trigger RestrictContactByName on Contact (before insert, before update){
/check contacts prior to insert or update for invalid data 
For(Contact c : Trigger.New){ 
if(c.LastName == 'INVALIDNAME'){ 
//invalidname is invalid  
c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');                
    }      
  } 
​}
Test Class(Apex class):
@isTest 
public class TestRestrictContactByName{ 
@isTest 
static void testRestrictContactbyName(){ 
test.startTest(); 
Contact con=new Contact(); 
con.FirstName='bccccc'; 
con.LastName='INVALIDNAME'; 
Insert con;
list<Contact> conlist=new list<Contact>{con}; 
Test.StopTest();
   } ​
​ }
By looking at the above example , create a text class and For your test class you have to follow the following steps:
 
Create a method Fisrt create a Case record..
//this will be the parent Case 
Case Parentcase = new Case(); 
  Parentcase.Status ='Working'; 
  Parentcase.Origin = 'Email'; 
  Parentcase.Number_of_Related_Cases__c = 0; 
Insert Parentcase; 
// Creating a Child Case 
Case Childcase = new Case(); 
  Childcase.Status = 'Working'; 
  Childcase.Origin = 'Email'; 
  Childcase.ParentId = Parentcase.Id; 
Insert Childcase; 
/* From above code the insert after insert event will be fired.*/ 
  Childcase.ParentId = Null; 
Update Childcase; 
/*From the above code your update event will be fired.*/ 
Make second method for the delete event
//this will be the parent Case 
Case Parentcase = new Case(); 
  Parentcase.Status ='Working'; 
  Parentcase.Origin = 'Email'; 
  Parentcase.Number_of_Related_Cases__c = 0;
Insert Parentcase;
//Child Case 
Case Childcase = new Case(); 
   Childcase.Status ='Working'; 
   Childcase.Origin = 'Email'; 
   Childcase.ParentId = Parentcase.Id;
 Insert Childcase; Delete Childcase;
/*From above code your delete event will be fired */
As you will write the code you should write test.startTest() at the beginning and Test.stopTest() at the end as shown in the above example.
This comes under best practices of writing test classes and for further knowledge you can go to the below link:-
https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_testing  

Regards
Ajay,
Please mark my answer as a solution if it is helpful.
This was selected as the best answer
Gary Payne 9Gary Payne 9
Ajay, thank you for the very thorough answer.  This is very helpful!
Regards,
Gary Payne