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
rafaelburirafaelburi 

How to close a Case with apex

Hi all.

 

I am trying to test a trigger that checks whether a Case is closed or not, but I can't close a Case in order to test my trigger.

I am doing the following in my test code.

 

Case parent = new Case();

Database.insert(parent);

 

Case child = new Case();

child.ParentId = parent.Id;

child.Status = 'Fechado'; // This label is set up to true in isClosed field in CaseStatus

Database.inset(child);

 

System.debug(child.Status); // It outputs Fechado

System.debug(child.isClosed); // It outputs false

 

Can someone help me? Sorry for my poor English.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

After you insert or update a record, you have to query the values back from the database. Only the ID value is updated in the memory reference for that record.

 

insert child;
child = [SELECT Id,Status,IsClosed FROM Case WHERE Id = :child.Id];

 

All Answers

sfdcfoxsfdcfox

After you insert or update a record, you have to query the values back from the database. Only the ID value is updated in the memory reference for that record.

 

insert child;
child = [SELECT Id,Status,IsClosed FROM Case WHERE Id = :child.Id];

 

This was selected as the best answer
rafaelburirafaelburi

Thanks fellow.

 

 

You are right.

Only the ID value is updated in the memory reference for that record.

I didn't know that.

 

Thanks again.