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
DVSDVS 

Update Child Cases when Parent Case is Updated

When a parent Case gets updated, how can I set up a trigger to update all the childcases? I don't need to update any of the fields on the child, just need the modified date to change.

 

Here is what I have so far but it is not working correctly.

 

trigger updateRelatedCasesfromParent on Case (after update) {
    
If(Trigger.isUpdate){
     
    Set<ID> ids = new Set<ID>();
     list<Case> updatedParents = [SELECT Id, reason FROM Case 
                 WHERE Id in :ids];
     List<Case> childrenToUpdate = new List<Case>();

         //Then loop through each parent object in 'updated parent
         for ( Case p : updatedParents) 
         { 
                //and loop thru each kid in the child set}
               for(Case kid : p.Cases) 
               { 
                    childrenToUpdate.add(kid);
                }
        }
       //if( !childrenToUpdate.isEmpty)
       //{
            update childrenToUpdate;
       //}
}
}

 Thanks for the help.

Best Answer chosen by Admin (Salesforce Developers) 
s_k_as_k_a

To modify fields on child cases or to set values  change if loop code like below.

if(parentIds.size()> 0)
	{
        for(Case ca : [Select Id , your field on case  from Case where ParentId IN :parentIds])
{
    c.modifiedfieldnmae = value;
    childrenToUpdate.add(c);
}
System.debug('---------------- Parent Case Received : ' + childrenToUpdate.size());
        try {
             System.debug('---------Going to update the parents ');
             update childrenToUpdate;
             System.debug('---------Parents updated successfully');
        } catch(Exception e) {
             System.debug('--------------Exception in updating all the Parents ') ;
        }
    }

 

All Answers

imutsavimutsav

Try this

trigger updateRelatedCasesfromParent on Case (after update) {

If(Trigger.isUpdate){
Set<Id> parentIds = Set<Id>();
for(Case c:Trigger.New) {
parentIds.add(c.Id);
}
List<Case> caseLists = [Select Id from Case where ParentId IN :parentIds];
update caseLists;
}

Thanks
Utsav

[Do mark this answer as solution and give KUDOS by clicking on the start below my name if this answer helps you resolve the issue]
DVSDVS

After implementing your code, I receive the following error when trying to update the parent.

 

Error:Apex trigger updateRelatedCasesfromParent caused an unexpected exception, contact your administrator: updateRelatedCasesfromParent: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 500g0000000dIYzAAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Count_Related_Cases: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 500g0000000dIYuAAM; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 500g0000000dIYu) is currently in trigger updateRelatedCasesfromParent, therefore it cannot recursively update itself: [] Trigger.Count_Related_Cases: line 48, column 1: []: Trigger.updateRelatedCasesfromParent: line 9, column 1

s_k_as_k_a

Try this code

 

trigger updateRelatedCasesfromParent on Case (after update){

if(trigger.isAfter &&  Trigger.isUpdate)
{
   List<Case> childrenToUpdate = new List<Case>();
   Set<Id> parentIds = Set<Id>();
   for(Case c:Trigger.New) {
         parentIds.add(c.ParentId);
   }
   if(parentIds.size()> 0)
	{
        childrenToUpdate =  [Select Id from Case where ParentId IN :parentIds];
        update caseLists;
    }
}

 

DVSDVS

With this, I no longer receive the error but it doesn't appear the trigger is actually updating the children. I have a Parent with two children and when I updated the Parent the modified date did not change on the children.

 

Also, if it makes for a easier solution, I have added a new custom Date/Time field that we can update to. I figure if the Parent is updated, we can update the children with datetime.now(); to that custom field.

s_k_as_k_a

Replace this line  "update caseLists" with  " update  childrenToUpdate;" then the code will work.

DVSDVS
Sorry, I already caught that. My comment above about the code not working was with that change made already.
DVSDVS

I had my trigger set to Before Update. I changed it to After Update and now I recieve this error when updating the parent.

 

Error:Apex trigger updateRelatedCasesfromParent caused an unexpected exception, contact your administrator: updateRelatedCasesfromParent: System.LimitException: Too many SOQL queries: 101

s_k_as_k_a

Put debug statement  in  if loop to find out the casess are adding to childrenToUpdate list

DVSDVS

I placed a debug statement in several places throughout the code, including the very first line and I'm not seeing anything being entered in the debug log.

imutsavimutsav
Try this : 

trigger updateRelatedCasesfromParent on Case (after update){ if(trigger.isAfter && Trigger.isUpdate) { List<Case> childrenToUpdate = new List<Case>(); Set<Id> parentIds = Set<Id>(); for(Case c:Trigger.New) {
System.debug('-------------Parent ID : ' + c.ParentId);
if(c.ParentId!=NULL) {
System.debug('----------Casse : + c.Name + ' Parent ----- ' + c.ParentId); parentIds.add(c.ParentId);
} }
System.debug('------------- Parent Ids -------- ' + parentIds); if(parentIds.size()> 0) { childrenToUpdate = [Select Id from Case where ParentId IN :parentIds];
System.debug('---------------- Parent Case Received : ' + childrenToUpdate);
try {
System.debug('---------Going to update the parents '); update childrenToUpdate;
System.debug('---------Parents updated successfully');
} catch(Exception e) {
System.debug('--------------Exception in updating all the Parents ') ;
} } }


[Do mark this answer as solution and give KUDOS by clicking on the start below my name if this answer helps you resolve the issue]
DVSDVS

Looks like the parent Ids are not populating. (only pasted part of the log since it exceeds the character limit I can put in a post.)

 

07:55:53.159 (159042000)|CODE_UNIT_FINISHED|Count_Related_Cases on Case trigger event AfterUpdate for [500g0000000dIYu]
07:55:53.168 (168732000)|CODE_UNIT_STARTED|[EXTERNAL]|01qg00000008UWI|updateRelatedCasesfromParent on Case trigger event AfterUpdate for [500g0000000dIYu]
07:55:53.168 (168978000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
07:55:53.169 (169009000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
07:55:53.169 (169033000)|SYSTEM_CONSTRUCTOR_ENTRY|[6]|<init>(Integer)
07:55:53.169 (169057000)|SYSTEM_CONSTRUCTOR_EXIT|[6]|<init>(Integer)
07:55:53.169 (169084000)|SYSTEM_METHOD_ENTRY|[7]|LIST<Case>.iterator()
07:55:53.169 (169117000)|SYSTEM_METHOD_EXIT|[7]|LIST<Case>.iterator()
07:55:53.169 (169130000)|SYSTEM_METHOD_ENTRY|[7]|system.ListIterator.hasNext()
07:55:53.169 (169145000)|SYSTEM_METHOD_EXIT|[7]|system.ListIterator.hasNext()
07:55:53.169 (169217000)|SYSTEM_METHOD_ENTRY|[8]|String.valueOf(Object)
07:55:53.169 (169243000)|SYSTEM_METHOD_EXIT|[8]|String.valueOf(Object)
07:55:53.169 (169263000)|SYSTEM_METHOD_ENTRY|[8]|System.debug(ANY)
07:55:53.169 (169275000)|USER_DEBUG|[8]|DEBUG|-------------Parent ID : null
07:55:53.169 (169281000)|SYSTEM_METHOD_EXIT|[8]|System.debug(ANY)
07:55:53.169 (169318000)|SYSTEM_METHOD_ENTRY|[7]|system.ListIterator.hasNext()
07:55:53.169 (169332000)|SYSTEM_METHOD_EXIT|[7]|system.ListIterator.hasNext()
07:55:53.169 (169363000)|SYSTEM_METHOD_ENTRY|[14]|String.valueOf(Object)
07:55:53.169 (169398000)|SYSTEM_METHOD_EXIT|[14]|String.valueOf(Object)
07:55:53.169 (169411000)|SYSTEM_METHOD_ENTRY|[14]|System.debug(ANY)
07:55:53.169 (169419000)|USER_DEBUG|[14]|DEBUG|------------- Parent Ids -------- {}
07:55:53.169 (169424000)|SYSTEM_METHOD_EXIT|[14]|System.debug(ANY)
07:55:53.169 (169436000)|SYSTEM_METHOD_ENTRY|[15]|SET<Id>.size()
07:55:53.169 (169465000)|SYSTEM_METHOD_EXIT|[15]|SET<Id>.size()
07:55:53.873 (169489000)|CUMULATIVE_LIMIT_USAGE
07:55:53.873|LIMIT_USAGE_FOR_NS|(default)|

 

s_k_as_k_a

Hello DVS,

 

your requirement is when the case update if that is parent all child cases are also need to update. is it correct?

 

In that case in your code If(c.parentId != null) line  adding onle child cases. thats y in debug log set showing null. Can you cahnge the code like below and post you debug log when parent case updated.

trigger updateRelatedCasesfromParent on Case (after update){

if(trigger.isAfter &&  Trigger.isUpdate)
{
   List<Case> childrenToUpdate = new List<Case>();
   Set<Id> parentIds = Set<Id>();
   for(Case c:Trigger.New) {
         system.debug('******CASE ID ::::'+ c.id +'--------- Case Parent ID:::::'+ c.parentId);
          parentIds.add(c.ParentId);
         }
   }
   System.debug('------------- Parent Ids -------- ' + parentIds);
   if(parentIds.size()> 0)
	{
        childrenToUpdate =  [Select Id from Case where ParentId IN :parentIds];
        System.debug('---------------- Parent Case Received : ' + childrenToUpdate.size());
        try {
             System.debug('---------Going to update the parents ');
             update childrenToUpdate;
             System.debug('---------Parents updated successfully');
        } catch(Exception e) {
             System.debug('--------------Exception in updating all the Parents ') ;
        }
    }
}

 

imutsavimutsav
My mistake, S_K_A is right we need to remove the if condition
if(c.ParentId!=NULL) {
System.debug('----------Casse : + c.Name + ' Parent ----- ' + c.ParentId);
parentIds.add(c.ParentId);
}
and leave this line

parentIds.add(c.ParentId);


Thanks
Utsav

[Do mark this answer as solution and give KUDOS by clicking on the start below my name if this answer helps you resolve the issue]
DVSDVS

Thanks, how can I place a value in a field now at the children?

s_k_as_k_a

To modify fields on child cases or to set values  change if loop code like below.

if(parentIds.size()> 0)
	{
        for(Case ca : [Select Id , your field on case  from Case where ParentId IN :parentIds])
{
    c.modifiedfieldnmae = value;
    childrenToUpdate.add(c);
}
System.debug('---------------- Parent Case Received : ' + childrenToUpdate.size());
        try {
             System.debug('---------Going to update the parents ');
             update childrenToUpdate;
             System.debug('---------Parents updated successfully');
        } catch(Exception e) {
             System.debug('--------------Exception in updating all the Parents ') ;
        }
    }

 

This was selected as the best answer
DVSDVS

 

Thanks for helping me through this guys. Few more questions though. The code below is not working properly. The children are not being updated. At the end of the debug log, I'm getting some "Internal Salesforce.com Errors."  Looks like the trigger might be looping through several times and causing problems instead of only going through once. Any ideas?

 

Also, I modified the code slightly since the previous version wouldn't let me save.

 

trigger updateRelatedCasesfromParent on Case (after update){

if(trigger.isAfter &&  Trigger.isUpdate)
{
   List<Case> childrenToUpdate = new List<Case>();
   Set<Id> parentIds = new Set<Id>();
   for(Case c:Trigger.New) {
         system.debug('******CASE ID ::::'+ c.id +'--------- Case Parent ID:::::'+ c.parentId);
         if (c.Is_Parent__c == 'Yes')
         {
              parentIds.add(c.Id);
          }
         }
   
   System.debug('------------- Parent Ids -------- ' + parentIds);
   if(parentIds.size()> 0)
    {
        for(Case ca : [Select Id , ParentUpdated__c from Case where ParentId IN :parentIds])
{
    ca.ParentUpdated__c = datetime.now();
    childrenToUpdate.add(ca);
}
System.debug('---------------- Parent Case Received : ' + childrenToUpdate.size());
        try {
             System.debug('---------Going to update the children ');
             update childrenToUpdate;
             System.debug('---------Children updated successfully');
        } catch(Exception e) {
             System.debug('--------------Exception in updating all the Parents ') ;
        }
    }
    }
}

 

DVSDVS

Nevermind guys! I just checked and it's one of the other triggers causing the problem now. Thanks for all the help with getting this one running. Much appreciated.