+ Start a Discussion
mallikammallikam 

question about parent and child cases

I have created some parent and respective child cases in Case object. Now, I want to make parent case not closable unless child cases have been closed..in other words, somebody should not be able to set the status of parent case to closed unless they have closed all the child cases. I am thinking to do this with triggers...but I have no idea how..any help is appreciated.
Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

This works for one case being closed via the UI:

 

 

trigger bfc_preventparentwhenchildopen on Case (before update) {

if (Trigger.New[0].Status=='Closed')

{integer close = [Select count() from Case where ParentId = :Trigger.New[0].id and isClosed != true];

if (close > 0)

{Trigger.New[0].addError('There are still Child Cases Open - Please close and try again!');

}

}

}

 

 

 

Message Edited by BritishBoyinDC on 07-17-2009 01:52 PM

All Answers

mallikammallikam

I have come up with a weird trigger code and this is not working.

 

trigger closeChildBeforeParent on Case (before update) { for (Case a: Trigger.old) { for (Case b:Trigger.old) { if ((a.status == 'closed') && (a.Id == b.ParentId) && (b.status != 'closed')) a.addError('You cannot close the Parent ticket unless all the related tickets have been closed'); } } }

 

Message Edited by mallikam on 07-17-2009 10:42 AM
BritishBoyinDCBritishBoyinDC

This works for one case being closed via the UI:

 

 

trigger bfc_preventparentwhenchildopen on Case (before update) {

if (Trigger.New[0].Status=='Closed')

{integer close = [Select count() from Case where ParentId = :Trigger.New[0].id and isClosed != true];

if (close > 0)

{Trigger.New[0].addError('There are still Child Cases Open - Please close and try again!');

}

}

}

 

 

 

Message Edited by BritishBoyinDC on 07-17-2009 01:52 PM
This was selected as the best answer
mallikammallikam
 Thanks!!!:smileyhappy:
Message Edited by mallikam on 07-17-2009 11:21 AM
Message Edited by mallikam on 11-19-2009 01:32 PM
gdhgdh

This sounds perfect for what we are trying to accomplish,  however i am a complete newbie and have never written a trigger before.  I've created the trigger in my sandbox environment using the code you provided.  Are there any other steps invloved, ie: do i also need to creat an APEX class?

mallikammallikam

No need to create any other classes, you should be all set. Why dont you try creating a parent case and child case and try to close the parent to see the trigger in action.

gdhgdh

Thanks, for the prompt response.  I have created a parent and child case.  Initially it allowed me to close the parent case while the child case was still open. 

 

I then noticed the following line in the code:

   >>>   if (Trigger.New[0].Status=='Closed')

 

But our process calls for either a closed status of "Soft Close" or "Hard Close". For testing purposes, I modified it so that we only have one closed status of "Closed" and it worked as we hoped.

 

Is there an operand that I can use instead of looking for an exact value of the status,  to one looking for a value that contains "close"?

 

mallikammallikam

There would be something like that but why dont you just try the straight values itself

 

if (Trigger.New[0].Status=='Soft Close') || (Trigger.New[0].Status=='Hard Close')

 

 

But if you have more options than these two, then may be you need to search for "close" string then.

gdhgdh

 

Thanks for the suggestion. I agree we only have those two options so evaluating for the straight values is straightforward,  however I receive a complie error:  unexpected token "||" when I add the modified if statement.

 

mallikammallikam
Can you send me the code block, where it is complaining?
gdhgdh

trigger bfc_preventparentwhenchildopen on Case (before update) {if (Trigger.New[0].Status=='Soft Close') || (Trigger.New[0].Status=='Hard Close') {integer close = [Select count() from Case where ParentId = :Trigger.New[0].id and isClosed != true]; if (close > 0) {Trigger.New[0].addError('There is/are still Child SR(s) Open - Please close and try again!'); } } }

 

mallikammallikam
if ((Trigger.New[0].Status=='Soft Close') || (Trigger.New[0].Status=='Hard Close'))...is what you should have for if statement...you just copied pasted my code..my code did not have enclosing brackets.
gdhgdh
THANKS, works perfectly.  Sorry for the rudimentary questions,  but like i said i'm a newbie at coding APEX triggers. Thanks again for your help and prompt responses.
gdhgdh

So I have received the go ahead to deploy this to our production environment after several users tested it in the sandbox environment. 

I’m using Eclipse and followed the instructions provided here: http://wiki.developerforce.com/index.php/Deploy_Force.com_Applications_Faster 

 

But the deployment fails with the following Messages recorded in the log file.

 

 *** Deployment Log ***

Result: FAILED

Date: November 30, 2009 1:30:04 PM CST 

 

# Deployed From:

   Project name: ParentChildCaseTrigger

   Username: gdh@smartdogservices.com.sandbox

   Endpoint: test.salesforce.com 

 

# Deployed To:  

   Username: gdh@smartdogservices.com  

   Endpoint: www.salesforce.com 

 

# Deploy Results:

   File Name:    package.xml

   Full Name:  package.xml

   Action:  UPDATED

   Result:  SUCCESS

   Problem: n/a   

   File Name:    triggers/bfc_preventparentwhenchildopen.trigger

   Full Name:  bfc_preventparentwhenchildopen

   Action:  UPDATED  

   Result:  SUCCESS

   Problem: n/a

 

# Test Results:  bfc_preventparentwhenchildopen Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

  Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required   

 

***Debug Log***

Cumulative profiling information:

 No profiling information for SOQL operations.

 No profiling information for SOSL operations.

 No profiling information for DML operations.

 No profiling information for method invocations.  

 

In an earlier post you mentioned that I didn’t need any additional classes,  but how do I get sufficient test coverage?

Message Edited by gdh on 11-30-2009 12:26 PM
mallikammallikam
You need to write test for this trigger...browse around or look at the APEX documentation to understand more about it.