+ Start a Discussion
Vishnu7700Vishnu7700 

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.

 

sfdcfoxsfdcfox
trigger caseTrigger on Case (before update) {
    Case[] closed = new Case[0], notclosed = new case[0];
    for(case record:trigger.new)
        (record.isclosed?closed:notclosed).add(record);
    if(!closed.isempty())
        for(case record:[select parentid from case where isclosed = false and parentid in :closed])
            trigger.newmap.get(record.parentid).status.adderror('Cannot close until all children are closed.');
}
Vishnu7700Vishnu7700

Hi,

Frankely speaking unable to understand logic can you explain it.

sfdcfoxsfdcfox
trigger caseTrigger on Case (before update) {

Before insert, no case can have a child case, because it does not yet exist. Therefore, we check only on updates.

 

    Case[] closed = new Case[0], notclosed = new case[0];

We would like to have two lists, a list of closed cases and not-closed (open) cases. This design simplifies unit testing.

 

    for(case record:trigger.new)
        (record.isclosed?closed:notclosed).add(record);

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(!closed.isempty())

If we found any closed cases, we'll query for their children.

 

        for(case record:[select parentid from case where isclosed = false and parentid in :closed])
            trigger.newmap.get(record.parentid).status.adderror('Cannot close until all children are closed.');

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:

 

trigger caseTrigger on Case (after update) {
    for(Case record:[SELECT ParentId FROM Case WHERE Parent.IsClosed = TRUE AND IsClosed = FALSE and ParentId IN :Trigger.new])
        Trigger.newMap.get(record.ParentId).Status.addError('You cannot close this case while any children are open.');
}

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.

Vishnu7700Vishnu7700

Hi,

while complie getting error as

Method does not exist or incorrect signature: [String].addError(String) at line 13

sfdcfoxsfdcfox
My code doesn't have a line 13. Could you post the code you're using?