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

Close a Case using Apex
For some reason I cannot get cases to close by setting the status to closed. The "isClosed" checkbox remains unchecked even if I change the status.
Here is my code:
trigger CloseRelatedOppCases on Opportunity (before update) { List<Opportunity> opps = Trigger.new; for(Opportunity o : opps){ if(o.StageName == 'Dead/Lost'){ //Get record type ID of Engineering Engagement Request RT, Credit Verification Request, and Network Qualification Request RecordType rt1 = [SELECT id FROM RecordType WHERE Name = 'Engineering Engagement Request' LIMIT 1]; RecordType rt2 = [SELECT id FROM RecordType WHERE Name = 'Credit Verification Request' LIMIT 1]; RecordType rt3 = [SELECT id FROM RecordType WHERE Name = 'Network Qualification Request' LIMIT 1]; //Find Associated NDS Cases List<Case> engineeringEngagementCases = [SELECT id FROM Case WHERE Opportunity__c = :o.Id AND RecordTypeId = :rt1.Id]; List<Case> creditVerificationCases = [SELECT id FROM Case WHERE Opportunity__c = :o.Id AND RecordTypeId = :rt2.Id]; List<Case> networkQualificationCases = [SELECT id FROM Case WHERE Opportunity__c = :o.Id AND RecordTypeId = :rt3.Id]; //Combine All Cases into one list List<Case> cases = new List<Case>(); cases.addall(engineeringEngagementCases); cases.addall(creditVerificationCases); cases.addall(networkQualificationCases); //Loop Through Cases and Set Status to Opportunity Dead/ for(Case c : cases){ c.Status = 'Opportunity Dead/Lost'; } update cases; } } }
The isClosed field is not writable, you have to change the status to one of your Closed statuses, the status is what controls the isClosed value.
--KC
All Answers
The isClosed field is not writable, you have to change the status to one of your Closed statuses, the status is what controls the isClosed value.
--KC
Right, that's what I did. I have a status of "Opportunity Dead/Lost" and I made sure it's one of the closed statuses however it still doesn't work.
I'm working in Winter '13, could this possibly be a bug?
Hi, I don't know if you've actually Solved your issue yet or not, but your trigger is flawed in that it's not bulk safe.
Hope this helps, Best, Steve.
p.s. Note that I've not tried this code, I'm just typing it in... so there are probably some typos, etc.
Thanks for the response but this still didn't solve my problem. Yes, It sets the status of all related cases to "Opportunity Dead/Lost" however it doesn't actually close the case.