You need to sign in to do that
Don't have an account?
DVS
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.
To modify fields on child cases or to set values change if loop code like below.
All Answers
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]
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
Try this code
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.
Replace this line "update caseLists" with " update childrenToUpdate;" then the code will work.
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
Put debug statement in if loop to find out the casess are adding to childrenToUpdate list
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.
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.)
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.
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]
Thanks, how can I place a value in a field now at the children?
To modify fields on child cases or to set values change if loop code like below.
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.
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.