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
Clement Debrueres-BartoliClement Debrueres-Bartoli 

Update Child Cases every time the Parent Case is edited (Case Trigger)

Hi Salesforce fellas,

I am still a beginner with Case Triggers and I would like to create an Apex Trigger that would update the children cases everytime the parent case is updated (any update on any field of the Parent).
* To make the parent-child relationship, I just use the standard method and enter the Parent Case number in "Parent Case" field of the Child Case.

The Children Cases would have their following fields updated with the value in the Parent Case:
Status, ClosedDate, Category, SubCategory, SubCategory2, Category_Bank, SubCategory_Bank

I made this Case Trigger  but it just does not work, and it is only when Parent Case ' Status' field is updated to "Closed"
 
trigger updateChildCases on Case (after update) {
  

        List<Case> childrenToUpdate = new List<Case>();
        Set<Id> parentIds = new Set<Id>();
        for(Case p:trigger.new) {
        
            if(p.IsClosed == TRUE) {
                parentIds.add(p.Id);
            }
        }
        if(parentIds.size() > 0) {
            for(Case ch : [SELECT Id, 
            Parent.Status,
            Parent.ClosedDate,
            
            Parent.Category__c,
            Parent.SubCategory_2__c, 
            Parent.SubCategory__c,
            
            Parent.Category_Bank__c,
            Parent.SubCategory_Bank__c  
            
            FROM Case WHERE Parent.Id IN :parentIds
            AND Parent.Status <> NULL
            AND PArent.ClosedDate <> NULL
            
            AND Parent.Category__c <> NULL
            AND Parent.SubCategory__c <> NULL
            AND Parent.SubCategory_2__c <> NULL
            
            AND Parent.Category_Bank__c <> NULL
            AND Parent.SubCategory_Bank__c <> NULL
            ]) {
 
                ch.Status = ch.Parent.Status;                 ch.ClosedDate = ch.Parent.ClosedDate;                 ch.Category__c = ch.Parent.Category__c;                 ch.SubCategory__c = ch.Parent.SubCategory__c;                 ch.SubCategory_2__c = ch.Parent.SubCategory_2__c;                ch.Category_Bank__c = ch.Parent.Category_Bank__c;                ch.SubCategory_Bank__c = ch.Parent.SubCategory_Bank__c;
 
                childrenToUpdate.add(ch);
            } 
Update childrenToUpdate;
            }
        }
my test class is probably incomplete:
@isTest
private class UpdateChildCases_Test {
 
  static testMethod void Test(){
            
    Case curCase = new Case(Status = 'Closed',
    Category__c ='Registration',
    SubCategory__c = 'Open/Reopen Account',
    SubCategory_2__c = 'Create Profile',
    Category_Bank__c = '1',
    SubCategory_Bank__c = '2' );
    insert curCase;
    
    Case curCase2 = new Case(Status='New',ParentId=curCase.Id);
    insert curCase2;

       Test.StartTest(); 
 
    Test.StopTest();
  }
}
(sorry if the code burns your eyes )

I get some code coverage and no error when I save the trigger and the test class, but then when I go back to my cases and try to close a Parent Case it just does not update the Child Case.

Any good soul who could help me with that please?

Thank you very much :)
Best Answer chosen by Clement Debrueres-Bartoli
@anilbathula@@anilbathula@
Hi Clement,

Try this code hope it works  for you :) 
 
trigger updateChildCases on Case (after update) {

        List<Case> childrenToUpdate = new List<Case>();        
        Map<id,Case>Mapofcase=new Map<id,case>();
        for(Case p:trigger.new) {
        
            if(p.status!=trigger.oldmap.get(p.id).status && p.IsClosed && 
			   p.Category__c!=NULL && p.ClosedDate!=null && p.SubCategory__c!=null &&
			   p.SubCategory_2__c!=null && p.Category_Bank__c!=null && p.SubCategory_Bank__c!=null) {
			   
                Mapofcase.put(p.Id,p);
                
            }
        }
        
        if(!Mapofcase.isempty()) {
         //query child fields which need to be update
            for(Case ch : [SELECT Id,Status,Parent.Id,Category__c,ClosedDate,SubCategory__c,SubCategory_2__c,Category_Bank__c,SubCategory_Bank__c FROM Case WHERE Parent.Id IN :Mapofcase.keyset()]) {
                        
              if(Mapofcase.containsKey(ch.Parent.Id)){
				ch.Status=Mapofcase.get(ch.Parent.Id).Status;					   
				ch.ClosedDate =Mapofcase.get(ch.Parent.Id).ClosedDate; 
				ch.Category__c = Mapofcase.get(ch.Parent.Id).Category__c;
				ch.SubCategory__c = Mapofcase.get(ch.Parent.Id).SubCategory__c;   
				ch.SubCategory_2__c = Mapofcase.get(ch.Parent.Id).SubCategory_2__c; 
				ch.Category_Bank__c = Mapofcase.get(ch.Parent.Id).Category_Bank__c;  
				ch.SubCategory_Bank__c =Mapofcase.get(ch.Parent.Id).SubCategory_Bank__c;
 
                childrenToUpdate.add(ch);
               }
              
            } 
            if(!childrenToUpdate.isempty())
            Update childrenToUpdate;
        }
}

Test clas:-
@isTest
private class UpdateChildCases_Test {
 
  static testMethod void Test(){
    Test.StartTest();  
	 
    Case curCase = new Case(Status = 'New',
    Category__c ='Registration',
    SubCategory__c = 'Open/Reopen Account',
    SubCategory_2__c = 'Create Profile',
    Category_Bank__c = '1',
    SubCategory_Bank__c = '2' );
    insert curCase;
    
    Case curCase2 = new Case(Status='New',ParentId=curCase.Id);
    insert curCase2;
	
	curCase.status='Closed';
	update curCase;
 
    Test.StopTest();
  }
}


Might be some typo errors please check before.


Thanks

Anil.B​​​​​​​
 

All Answers

@anilbathula@@anilbathula@
Hi Clement,

Try this code hope it works  for you :) 
 
trigger updateChildCases on Case (after update) {

        List<Case> childrenToUpdate = new List<Case>();        
        Map<id,Case>Mapofcase=new Map<id,case>();
        for(Case p:trigger.new) {
        
            if(p.status!=trigger.oldmap.get(p.id).status && p.IsClosed && 
			   p.Category__c!=NULL && p.ClosedDate!=null && p.SubCategory__c!=null &&
			   p.SubCategory_2__c!=null && p.Category_Bank__c!=null && p.SubCategory_Bank__c!=null) {
			   
                Mapofcase.put(p.Id,p);
                
            }
        }
        
        if(!Mapofcase.isempty()) {
         //query child fields which need to be update
            for(Case ch : [SELECT Id,Status,Parent.Id,Category__c,ClosedDate,SubCategory__c,SubCategory_2__c,Category_Bank__c,SubCategory_Bank__c FROM Case WHERE Parent.Id IN :Mapofcase.keyset()]) {
                        
              if(Mapofcase.containsKey(ch.Parent.Id)){
				ch.Status=Mapofcase.get(ch.Parent.Id).Status;					   
				ch.ClosedDate =Mapofcase.get(ch.Parent.Id).ClosedDate; 
				ch.Category__c = Mapofcase.get(ch.Parent.Id).Category__c;
				ch.SubCategory__c = Mapofcase.get(ch.Parent.Id).SubCategory__c;   
				ch.SubCategory_2__c = Mapofcase.get(ch.Parent.Id).SubCategory_2__c; 
				ch.Category_Bank__c = Mapofcase.get(ch.Parent.Id).Category_Bank__c;  
				ch.SubCategory_Bank__c =Mapofcase.get(ch.Parent.Id).SubCategory_Bank__c;
 
                childrenToUpdate.add(ch);
               }
              
            } 
            if(!childrenToUpdate.isempty())
            Update childrenToUpdate;
        }
}

Test clas:-
@isTest
private class UpdateChildCases_Test {
 
  static testMethod void Test(){
    Test.StartTest();  
	 
    Case curCase = new Case(Status = 'New',
    Category__c ='Registration',
    SubCategory__c = 'Open/Reopen Account',
    SubCategory_2__c = 'Create Profile',
    Category_Bank__c = '1',
    SubCategory_Bank__c = '2' );
    insert curCase;
    
    Case curCase2 = new Case(Status='New',ParentId=curCase.Id);
    insert curCase2;
	
	curCase.status='Closed';
	update curCase;
 
    Test.StopTest();
  }
}


Might be some typo errors please check before.


Thanks

Anil.B​​​​​​​
 

This was selected as the best answer
Clement Debrueres-BartoliClement Debrueres-Bartoli
Hi Thanks a lot Anil, I managed to get 100% with your test Class

I kept my Trigger like that: everytime Parent Case is closed, Children Cases'  fields are updated
 
trigger updateChildCases on Case (after update) {

        List<Case> childrenToUpdate = new List<Case>();
        Set<Id> parentIds = new Set<Id>();
        
        for(Case p:trigger.new) {
        
            if(p.IsClosed == TRUE) {
                parentIds.add(p.Id);
            }
        }
        if(parentIds.size() > 0) {
            for(Case ch : [SELECT 
            Id, 
            Parent.Status,
            Parent.Category__c,
            Parent.SubCategory_2__c, 
            Parent.SubCategory__c,
            Parent.MainIssueDescription__c,
            Parent.Category_Bank__c,
            Parent.SubCategory_Bank__c
            
            FROM Case WHERE 
            Parent.Id IN :parentIds
            AND Parent.Status <> NULL 
            AND (Parent.Category__c <> NULL OR Parent.Category_Bank__c <> NULL)

            ]) {
 
                ch.Status = ch.Parent.Status;
                ch.Category__c = ch.Parent.Category__c;
                ch.SubCategory__c = ch.Parent.SubCategory__c;
                ch.SubCategory_2__c = ch.Parent.SubCategory_2__c;
                ch.MainIssueDescription__c = ch.Parent.MainIssueDescription__c;
                ch.Category_Bank__c = ch.Parent.Category_Bank__c;
                ch.SubCategory_Bank__c = ch.Parent.SubCategory_Bank__c;
                
                childrenToUpdate.add(ch);
            }
            Update childrenToUpdate; 
            }
        }
and the test class is:
 
@isTest
private class UpdateChildCases_Test {
 
  static testMethod void Test(){
            Test.StartTest(); 
            
    Case curCase = new Case(Status = 'Closed',
    Category__c='Registration',
            
            Category_Bank__c = 'Bank Queries',
            SubCategory_Bank__c = 'Account'
            );
    insert curCase;
    
    Case curCase2 = new Case(Status='New',ParentId=curCase.Id,Category__c='Registration',
            
            Category_Bank__c = 'Payments Queries',
            SubCategory_Bank__c = 'Withdrawal');
    insert curCase2;
    
       curCase.status='Closed';
        update curCase;
 
    Test.StopTest();
  }
}
 All working, thank you again and have a nice one !