You need to sign in to do that
Don't have an account?
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();
}
}
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
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];
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!