You need to sign in to do that
Don't have an account?
mallikam
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.
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!');
}
}
}
All Answers
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'); } } }
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!');
}
}
}
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?
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.
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"?
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.
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.
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!'); } } }
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?