function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike @ BlackTabMike @ BlackTab 

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;
		}
	}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
kcpluspluskcplusplus

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

kcpluspluskcplusplus

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

This was selected as the best answer
Mike @ BlackTabMike @ BlackTab

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. 

Mike @ BlackTabMike @ BlackTab

I'm working in Winter '13, could this possibly be a bug?

SteveBowerSteveBower

 

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.

 

trigger CloseRelatedOppCases on Opportunity (before update) {
// Make a list of the Dead/Lost Opportunities in this Trigger invocation
List<Opportunity> DeadOrLost = new List<Opportunity>();
for (Opportunity o: Trigger.new)
// We only care about this Opp if the Stagename has been changed, and it's new value is Dead/Lost
if (
(o.StageName == 'Dead/Lost') and
(Trigger.oldMap.get(o.id).StageName <> 'Dead/Lost')
) DeadOrLost.add(o);
if (DeadOrLost.isEmpty()) return;

// Get Record Types:
// Engineering Engagement Request, Credit Verification Request, and Network Qualification Request
List<Id> RecordTypes = [SELECT id FROM RecordType WHERE sobjecttype = "Case" and (
Name = 'Engineering Engagement Request' or
Name = 'Credit Verification Request' or
Name = 'Network Qualification Request')];

// Get all the Cases that we're going to want to update
List<Case> CasesToUpdate = [Select Status from Case where
Opportunity__c in :DeadOrLost and
Status <> 'Opportunity Dead/Lost' and
RecordTypeId in :RecordTypes]);
if (CasesToUpdate.isEmpty()) return;

// If we have any, just update them all
for (case c: CasesToUpdate) c.Status = 'Opportunity Dead/Lost';
update CasesToUpdate;
}

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.

Mike @ BlackTabMike @ BlackTab

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.