+ Start a Discussion
OssieOssie 

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! 
Best Answer chosen by Ossie
ShashForceShashForce
Hi,

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"

trigger CloseCases on Test_Custom__c (after insert, after update) {
    list<Id> IdsList = new list<Id>();
    list<case> CasesToUpdate = new list<case>();
    
    for(test_custom__c tc:trigger.new){
        IdsList.add(tc.Id);
    }
    list<case> cases = [select Id,status,test_custom__c from case where test_custom__c in :IdsList];
    
    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);
            }
        }
    }
    update CasesToUpdate;
}

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

Sonam_SFDCSonam_SFDC
Hi Ossie,

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
ShashForceShashForce
Hi,

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"

trigger CloseCases on Test_Custom__c (after insert, after update) {
    list<Id> IdsList = new list<Id>();
    list<case> CasesToUpdate = new list<case>();
    
    for(test_custom__c tc:trigger.new){
        IdsList.add(tc.Id);
    }
    list<case> cases = [select Id,status,test_custom__c from case where test_custom__c in :IdsList];
    
    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);
            }
        }
    }
    update CasesToUpdate;
}

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
This was selected as the best answer
OssieOssie
Thank you so much Shashank.  Your coding worked perfectly. Could i please ask for one last favour.  I have created my test class and have 72% code coverage.  The only bit of coding that has not been covered is: 

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
ShashForceShashForce
Can you please post your test class?
OssieOssie
Yes of course.

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);
ShashForceShashForce
Please try this:

@isTest       

    private class TestCloseCases {
   
        public static testMethod void testCloseCases() {
            Test_Custom__c B = new Test_Custom__c(Name='12345-1');
            insert B;
                       
            Case c=new Case(Priority='Medium', Test_Custom__c=B.id, Take_Ownership__c=true);
            insert c;
            
            B.oppstage__c = 'Perception Analysis';
            update B;
                      
            }
}

Initially, you were inserting the custom object record, but inserting case record only after the trigger is fired on the insert.
OssieOssie
Excellent. You Legend!
Thank You!