+ Start a Discussion
viswsanathviswsanath 

Hi All, is it possible we can write a soql querys in test class

Hi All,
is it possible we can write a soql querys in test class .

Regards,
Viswa:)
Mani RenusMani Renus

Yes, it is possible!! but you need write SeeAllData=true(as like below) for access org data.

@isTest(SeeAllData=true)

Rahul SharmaRahul Sharma
Ideally you should create your own test data!
viswsanathviswsanath
Sorry am not getting your point can you pls domonstrate on that
Rahul SharmaRahul Sharma
You could learn more on writing test classes in Awesome Salesforce Trailhead - Getting Started with Apex Unit Tests (https://developer.salesforce.com/trailhead/apex_testing/apex_testing_intro) and Creating Test Data for Apex Tests (https://developer.salesforce.com/trailhead/apex_testing/apex_testing_data)
jyothsna reddy 5jyothsna reddy 5
Hi Viswsanath,
    Yes,it is possible to write  a soql in test class using @IsTest(SeeAll=True) Annotation.
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, this annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the testmethod keyword.
  • The isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level. However, using isTest(SeeAllData=false) on a method doesn’t restrict organization data access for that method if the containing class has already been defined with the isTest(SeeAllData=true) annotation. In this case, the method will still have access to all the data in the organization. 
    For more information please follow the below link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm