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
Vishvesh BhandariVishvesh Bhandari 

Why to avoid seeAllData?

Hey All,
Just a small question, Why is it recommended to avoid using "seeAllData" in the Test classes?

Thanks in advance.
Vishvesh
 
Chandrashekhar GoudChandrashekhar Goud

Hi Bhandari

This is because of following reasons -
1) Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access 
by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings 
data, and can only access data that they create.

2) You must create test data for each test. You can disable this restriction by annotating your test class or 
test method with the IsTest(SeeAllData=true) annotation. For more information, see IsTest(SeeAllData=true) 
Annotation.

3) 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.

4) Also If you use the (SeeAllData=true) , it might happen that test class pass in sandbox but fails in production while deployment
 

Mohit Kumawat 20Mohit Kumawat 20
Hi Vishvesh,
Test classes run in a different context, i.e. a test class have no idea about the data stored in Salesforce, by setting seeAllData=true =>
@isTest(seeAllData = true)
Enabling this attribute exposes the data from the database to the test class.
It is recommended not to use as the code coverage of you apex class or trigger will now be dependent on the data which is present in org and depending upon that the code coverage may change.

Mohit Kumawat 
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts