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
MoroYMoroY 

Reopening child cases

Hello everyone, I am quite new on the platform and have a rather interesting task to solve. I have been playing around with a Trigger but I have not gotten that far. Help is much appreciated.

For Cases:
1. If the parent case is closed, I would like to prevent the child case status from changing from closed to open.
2. A generic error message should be displayed informing the user of this
3. When child case status has been changed from closed to open, the child case cannot be saved if the parent case is not open
4. When child case status has been changed from closed to open, the child case can be saved if parent case also has status of open
Best Answer chosen by MoroY
AnkaiahAnkaiah (Salesforce Developers) 
Hi Moro,

try with beloe validation rule.
AND( ISPICKVAL(Parent.Status,'Closed'),ISCHANGED( Status ), ISPICKVAL(PRIORVALUE(Status),'closed'),ISPICKVAL(Status,'Open') )

If this helps, Please mark it as best answer.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Moro,

try with beloe validation rule.
AND( ISPICKVAL(Parent.Status,'Closed'),ISCHANGED( Status ), ISPICKVAL(PRIORVALUE(Status),'closed'),ISPICKVAL(Status,'Open') )

If this helps, Please mark it as best answer.

Thanks!!
This was selected as the best answer
Shubham Jain 338Shubham Jain 338
Hi MoroY,

You can simply create a validation rule to prevent this. There is no need to write a logic in the trigger.
Still, if you are curious how we can do that in trigger then please find the solution

trigger CaseTrigger on Case (before update) {

    Set<Id> caseIdSet = new Set<Id>();
    List<Case> caseList = new List<Case>();
    for (Case cs : Trigger.new) {
        if (cs.Status != Trigger.oldMap.get(cs.Id).Status && cs.Status == 'Open' && cs.ParentId != null) {
            caseIdSet.add(cs.Id);
            caseList.add(cs);
        }
    }
    if (caseIdSet.size() > 0) {
        //Either query the same case with parent status or query the parent case. Here I am querying the same case 
        Map<Id,Case> caseMap = new Map<Id,Case>([SELECT Id, Parent.Status FROM Case WHERE Id IN :caseIdSet]);
        for (Case cs : caseList) {
            if (caseMap.containsKey(cs.Id) && caseMap.get(cs.Id).Parent.Status == 'Closed') {
                cs.addError('You can not open the case as parent case is already closed');
            }
        }
    }
}


Please mark this as the best answer if it helps

Thanks
Shubham Jain
MoroYMoroY
Thank you so much for both your answers. Indeed Ankaiah, a validation is the way to go and the one you've provided works well.
Thank you Shubhaim for taking the time to create the Trigger example.
MoroYMoroY
Hello Ankaiah, just a follow up on that rule.
Although intitially the Child case status is prevented from being saved from Closed to Open if the Parent status is Closed, if I pick a different value for the Child case other that Closed (for example Processing), and then save as Open, it is saved. Even though the Parent status remains Closed. 
AnkaiahAnkaiah (Salesforce Developers) 
Are you updating the child case status from closed to Processing and then from processing to open?

If yes, then validation rule will not work. it will fire only when change the status from closed to open.

If there any chance to modify the status from closed to other than open then we need to include the remaing conditions in validation rule.

try with below.
AND( 
ISPICKVAL(Parent.Status,'Closed'),
ISCHANGED( Status ), 
OR(ISPICKVAL(PRIORVALUE(Status),'closed'),ISPICKVAL(Status,'Open'),
   ISPICKVAL(PRIORVALUE(Status),'closed'),ISPICKVAL(Status,'Processing'),
   ISPICKVAL(PRIORVALUE(Status),'closed'),ISPICKVAL(Status,'piclist3')
   ))

If you don't want to change the chail case status  when parent case is closed. try with below.
AND( 
ISPICKVAL(Parent.Status,'Closed'),
ISCHANGED( Status ))

Let me know if any other changes required.

Thanks!!


 
MoroYMoroY
After a bit of testing around, I realised the extra picklist option ( ex: Processing), can be added as || , and it seems to have solved it. 
EX: 

AND( ISPICKVAL(Parent.Status,'Closed'),ISCHANGED( Status ), ISPICKVAL(PRIORVALUE(Status),'closed'),
|| ISPICKVAL(PRIORVALUE(Status),'Processing'), ISPICKVAL(Status,'Open') )
MoroYMoroY
Thank you for the help Ankaiah, I've changed a few things round and all seems to be working good.