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
Saurabh Sood 12Saurabh Sood 12 

How to access database records inside test method

I'm writitng test class for this method but unable to get inside  if(pr.size() > 0)  condition.
Can anyone help me out how to resolved it .

public void fetchPriceDetails(){
           for(Product__c p : prod)
               pr = [SELECT List_Price__c FROM Pricing__c WHERE  Location__c =: p.Location__c AND Sub_Product__c =: p.Sub_Product__c AND Product_Detail__c =: p.Product_Details__c  AND  Duration_Type__c  =: p.Select_Duration__c AND Centre__c =: p.Centre__c];
             if(pr.size() > 0){

               for(Pricing__c d: pr){
                  if(d.List_Price__c != null || d.List_Price__c != 0)
                       p.List_Price_1__c = pr[0].List_Price__c;
        catch(Exception e)
            // ApexPages.addMessages(e);

Khan AnasKhan Anas (Salesforce Developers) 
Hi Saurabh,

Greetings to you!

Annotate your test class or test method with IsTest(SeeAllData=true) to open up data access to records in your organization.

Considerations for the @IsTest(SeeAllData=true) Annotation
  • If a test class is defined with the @isTest(SeeAllData=true) annotation, the annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the (deprecated) testMethodkeyword.
  • The @isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level. However, if the containing class has been annotated with @isTest(SeeAllData=true), annotating a method with @isTest(SeeAllData=false) is ignored for that method. In this case, that method still has access to all the data in the organization. Annotating a method with @isTest(SeeAllData=true) overrides, for that method, an @isTest(SeeAllData=false) annotation on the class.
  • @isTest(SeeAllData=true) and @isTest(isParallel=true) annotations cannot be used together on the same Apex method.

Always try to avoid use SeeAllData= true because in that case, your test class depends on your sandbox data. You may get some issue will deployment. Instead of using SeeAllData=true, create test data in your test class.

Please refer to the below links which might help you further with the above requirement.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas

Saurabh Sood 12Saurabh Sood 12
Hi Anas, 
Thanks for reply,

My question is Pricing__c  is object where all products prices are stored and in logic i'm querying forn database. Right !!.
and I created records for pricing in test class. so how can I quering records to store value in pr list  so it can assign value for product pricing.