You need to sign in to do that
Don't have an account?

Close all Cases when the Status of a field changes in a Custom Object
Hi All,
I would like to implement some functionality which Closes all Cases when the status field is changed to a particulaur value in my custom object. The Cases object is linked to the custom object via a Lookup.
I would imagine that this can only be achieved through Apex coding (triggers?) and if so not sure how to start?
Would apprciate any help!
I would like to implement some functionality which Closes all Cases when the status field is changed to a particulaur value in my custom object. The Cases object is linked to the custom object via a Lookup.
I would imagine that this can only be achieved through Apex coding (triggers?) and if so not sure how to start?
Would apprciate any help!
This trigger sample code should work for your requirement. I wrote this code taking following example:
custom object API name: test_custom__c
custom field on custom object: oppstage__c
picklist value of custom field which will close cases: "Perception Analysis"
case close status: "Closed"
If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.
Thanks,
Shashank
All Answers
You can create an after insert after update trigger on that custom Object where the field is located which controls this closure of cases.
In this trigger - get all the cases associated with this custom object record using SOQL.
sample code on the threads below:
http://salesforce.stackexchange.com/questions/11100/trigger-to-update-account-from-case-field
https://developer.salesforce.com/forums/ForumsMain?id=906F00000009P0oIAE
This trigger sample code should work for your requirement. I wrote this code taking following example:
custom object API name: test_custom__c
custom field on custom object: oppstage__c
picklist value of custom field which will close cases: "Perception Analysis"
case close status: "Closed"
If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.
Thanks,
Shashank
for(test_custom__c t:[select Id,oppstage__c from test_custom__c where Id IN :IdsList]){
for(case cs:cases){
if((cs.test_custom__c == t.Id)&&(t.oppstage__c == 'Perception Analysis')){
cs.status = 'Closed';
CasesToUpdate.add(cs);
Any idea what i need to specifiy in my test class to test these lines?
Regards
Using your example of Test_custom__c:
@isTest
private class TestCloseCases {
public static testMethod void testCloseCases() {
Test_Custom__c B = new Test_Custom__c(Name='12345-1',oppstage__c = 'Perception Analysis');
insert B;
Case c=new Case(Priority='Medium', Test_Custom__c=B.id, Take_Ownership__c=true);
insert c;
update c;
}
}
And correction its only the following 3 lines that need code coverage:
if((cs.test_custom__c == t.Id)&&(t.oppstage__c == 'Perception Analysis')){
cs.status = 'Closed';
CasesToUpdate.add(cs);
Initially, you were inserting the custom object record, but inserting case record only after the trigger is fired on the insert.
Thank You!