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
Kals_KalsKals_Kals 

Not able to fetch from record inserted through Test Case

Hello All,
Here is my test class. I am inserting a record into standard object Case. After insertion, when I am trying to fetch the value of case number, it returns null. But the assert statement works fine. Test class works fine.
Why am I not getting the value for the case that is inserted? Any help is greatly appreciated.
Thanks.

private class TestMyTriggerHandler {

private static Id cNumber;

 

     static testMethod void myUnitTest() {

    Test.startTest();

          Case newCase = new Case( Solution__c = 'Test test test'     Description = 'Test test test');

       

        insert newCase;

       System.assert(newCase != null);

       

cNumber = newCase.CaseNumber;

      System.debug('Case Number is ' +cNumber);       

       Test.stopTest(); 

   }

}

Best Answer chosen by Admin (Salesforce Developers) 
MJ Kahn / OpFocusMJ Kahn / OpFocus

After you insert the case, you have to query the database to get its values; the insert operation gives you only the Case Id. Add this after your insert:

 

newCase = [select CaseNumber from Case where Id = :newCase.id];

All Answers

MJ Kahn / OpFocusMJ Kahn / OpFocus

After you insert the case, you have to query the database to get its values; the insert operation gives you only the Case Id. Add this after your insert:

 

newCase = [select CaseNumber from Case where Id = :newCase.id];

This was selected as the best answer
Kals_KalsKals_Kals

I did try it last night after a long wait and it did really work. Thanks for your reply. I have marked it as solution so that it will be beneficial for others. Thanks!