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
RoyGiladRoyGilad 

Querying Master Record in test class vs. in DevConsole

Hi,
We have a Test class the query a Master record deatils and get nulls only in the test class, in "real" run (via annanumence code run)  - it does get values.

Any idea?

Code:
Success__c suc = (Success__c)SmartFactory.createSObject('Success__c');
suc.Account__c=acc.Id;
suc.Opportunity__c=opp.Id;
User successOwner=[select Id,LastName,IsActive from User where UserRole.Name like '%CSM%' and IsActive=true limit 1];
system.debug('Tal Carmi emvcsmic successOwner: '+successOwner);
suc.OwnerId=successOwner.Id;
insert suc;
        
suc=[select Id,OwnerId,Owner.LastName,Owner.IsActive from Success__c where Id=:suc.Id];
system.assertEquals(successOwner.Id,suc.OwnerId);
        
system.debug('Tal Carmi emvcsmic suc: '+suc);
system.debug('Tal Carmi emvcsmic suc.OwnerId: '+suc.OwnerId);
system.debug('Tal Carmi emvcsmic suc.Owner.LastName: '+suc.Owner.LastName);
system.debug('Tal Carmi emvcsmic suc.Owner.IsActive: '+suc.Owner.IsActive);
Log (in test class run):
User-added image
Dilip_VDilip_V
Roy,

Have you used this annotaion?If you want to access your org data in test class use this annotation. 
@isTest(SeeAllData=true)

Like this in class.
// All test methods in this class can access all data.
@isTest(SeeAllData=true)
public class TestDataAccessClass {

    // This test accesses an existing account. 
    // It also creates and accesses a new test account.
    static testmethod void myTestMethod1() {
        // Query an existing account in the organization. 
        Account a = [SELECT Id, Name FROM Account WHERE Name='Acme' LIMIT 1];
        System.assert(a != null);
        
        // Create a test account based on the queried account.
        Account testAccount = a.clone();
        testAccount.Name = 'Acme Test';
        insert testAccount;
        
        // Query the test account that was inserted.
        Account testAccount2 = [SELECT Id, Name FROM Account 
                                WHERE Name='Acme Test' LIMIT 1];
        System.assert(testAccount2 != null);
    }
       
    
    // Like the previous method, this test method can also access all data
    // because the containing class is annotated with @isTest(SeeAllData=true).
    @isTest static void myTestMethod2() {
        // Can access all data in the organization.
   }
  
}

However its  not best practice.Insert some test data in the test class and then query.

For more info:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm

If it helps make it as best answer.

Thanks.
RoyGiladRoyGilad
Thanks, but I'm creating the records in the Test class so they should be visible when runnning.

I suggest avoiding "SeeAllData=true".
Alex SelwynAlex Selwyn
@RoyGilad We encountered similar issue with a custom object post Spring'16 release, I believe in our case the record never got inserted with no error or exception. We got it resolved after opening a case with salesforce.

Verify sharing settings, any trigger exception (the usual stuffs) and then open a case with salesforce.