+ Start a Discussion
Santosh AlleSantosh Alle 

Why not to use (SeeAllData == true) in Test class

Hello Devs,

I was going through the blogs and salesforce documentation and few blogger's mentioned to avoid using (SeeAllData == true) while writing test class. I am not able to understand the exact reason for this. Can anyone explain it?

Thanks in advance.
Best Answer chosen by Santosh Alle
SantoshChitalkarSantoshChitalkar
Hi Santosh,

This is because of following reason -
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

regards,
Santosh Chitalkar

All Answers

PratikPratik (Salesforce Developers) 
Hi Santosh,

SeeAllData == true will give access to all records in your org. But it is always recomended and as per best practices, you should create the test records in the test classes insted of referring the existing records in org.

Thanks,
Pratik
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Santosh Alle,

Adding to what pratik said, try to make your test class independent of your org data using SeeAllData =false whereever possible

Reason : While moving your code from one org to another org (Developer to Staging Integration to Production) ,there may be some sandboxes without data ,and your test classes with SeeAllData == true might fail there.

Let us know if it helps you.
 
SantoshChitalkarSantoshChitalkar
Hi Santosh,

This is because of following reason -
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

regards,
Santosh Chitalkar
This was selected as the best answer
Santosh AlleSantosh Alle
Thank you Ashish, Pratik and Santosh.