You need to sign in to do that
Don't have an account?
Vishnu7700
Trigger complied successfully but funcationality was not achived
Hi,
My requriment is to write trigger for scenario Admin wants parent case cant be closed until all child cases are closed.
Below is my code
trigger Casecannotcloseifchildcsaeisopen on Case (before insert) { List<Case> lstcase = new List<Case>(); Set<String> setcase = new Set<String>(); for(Case c : Trigger.new){ if(c.Status == 'Closed'){ setcase.add(c.Id); } } lstcase = [Select Id from Case where id in:setcase]; Case childcase = new Case(); if(setcase.size()>0){ childcase.adderror('Can not delete case if child csae is open'); } }
Can any one help out.
Hi,
Frankely speaking unable to understand logic can you explain it.
Before insert, no case can have a child case, because it does not yet exist. Therefore, we check only on updates.
We would like to have two lists, a list of closed cases and not-closed (open) cases. This design simplifies unit testing.
Separate cases into either closed or not-closed. Case.IsClosed is a boolean value, so we don't need to check against true or false. The ternary operator "a?b:c" is a shorthand notation for "if(a) b else c", which means that either the case is closed and we will add it to the closed list, or the case is not closed and we will add it to the not-closed list.
If we found any closed cases, we'll query for their children.
We will query all open children cases for all parent closed cases to see if there are any matches. If so, we add an appropriate error message.
Thinking about it, you could also just do this:
This has a downside in that we can't short-circuit the query (e.g. if(!closed.isempty())), but on the other hand, the code is reduced to just four lines.
Hi,
while complie getting error as
Method does not exist or incorrect signature: [String].addError(String) at line 13