You need to sign in to do that
Don't have an account?
Clement 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"
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 :)
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 :)
Try this code hope it works for you :)
Test clas:-
Might be some typo errors please check before.
Thanks
Anil.B
All Answers
Try this code hope it works for you :)
Test clas:-
Might be some typo errors please check before.
Thanks
Anil.B
I kept my Trigger like that: everytime Parent Case is closed, Children Cases' fields are updated
and the test class is:
All working, thank you again and have a nice one !