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

Querying Master Record in test class vs. in DevConsole

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?

Success__c suc = (Success__c)SmartFactory.createSObject('Success__c');
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);
insert suc;
suc=[select Id,OwnerId,Owner.LastName,Owner.IsActive from Success__c where Id=:suc.Id];
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

Have you used this annotaion?If you want to access your org data in test class use this annotation. 

Like this in class.
// All test methods in this class can access all data.
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:

If it helps make it as best answer.

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.