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
praveen kumar 110praveen kumar 110 

How do we get casenumber in case object while inserting test data into case object,?

Best Answer chosen by praveen kumar 110
KRayKRay
Hey Praveen, 

1.) Use a service class to create the test case.  
2.) In your test class, call the function/method from step #1.
3.) Now use SOQL to search for the test case number.  

All Answers

KRayKRay
Hey Praveen, 

1.) Use a service class to create the test case.  
2.) In your test class, call the function/method from step #1.
3.) Now use SOQL to search for the test case number.  
This was selected as the best answer
praveen kumar 110praveen kumar 110
Hi Kray,

 Please expalin clearly,i didnot undestand,please share code if you so that i can understand
KRayKRay
It depends on the level of testing that you're trying to achieve. If you create a case with ONLY the required fields populated, the code below should work. If you want to test the account and contact fields, you'll have to use the method that I listed in the previous post. 
@isTest

public class testCaseForForum{

    public static void createTestCase(){
    
            
       Case testCase = new Case(
        Status ='New',
        Origin='Phone',
        Subject='This is a test case for testCaseForForum');
        insert(testCase);
    }

    public static void getCaseNumber(){
    
    //Use line below to retrieve the entire case
    Case testCase = [SELECT CaseNumber, Status, Origin, Subject FROM Case WHERE Subject='This is a test case for testCaseForForum'];
     
    //Use line below to retrieve the case number only
    String testCaseNumber = [SELECT CaseNumber FROM Case WHERE Subject='This is a test case for testCaseForForum'].CaseNumber;  
    
    }


}