[SalesForce] CaseNumber is not visible after case creation

I am having a facing a problem with CaseNumber.

I am creating a case object with all the mandatory field in my apex code.

 Case caseObject = new Case(DBG_Country__c = country,                                  
                               Description = descp
                         ......);
 insert case;
 System.debug('*****CaseNumber'+caseObject.CaseNumber);

In the above debug the CaseNumber is coming as null. Isn't case number generated when the case is created? just like the case record id? Then why am I not being able to view it in the next step?

Best Answer

You have to execute a query to retrieve any fields that are populated automatically when you perform DML, except Id. Id is the only field the system will automatically populate in your sObjects when you perform an insert.

Here, you must do

caseObject = [SELECT CaseNumber FROM Case WHERE Id = :caseObject.Id];
Related Topic