Owner related fields are not retrieved via SOQL on custom object in Apex test using dat silo
Last updated 2017-02-10 ·Reference W-1646262 ·Reported By 21 users
No Fix
Summary
In Apex test method using data silo (API version >= 24.0 or SeeAllData=true are used), SOQL queries on custom objects don't return User fields via the relationship name "Owner".
Repro
1) Create Apex test class that contains below test method:
//Don't use (SeeAllData=true) for version 24.0 and later
static testmethod void mytestmethod() {
Customobject__c co = new Customobject__c(Name='abctest', OwnerId = UserInfo.getUserId());
insert a;
List<Customobject__c> co_list = [SELECT id, Owner.ID, Owner.IsActive, Owner.Name FROM customobject__c WHERE id =:co.id];
system.Debug('\n### OUTPUT ##'+
'\n OUTPUT OWNER ISACTIVE : '+co_list[0].Owner.IsActive+
'\n OUTPUT OWNER ID : '+co_list[0].Owner.ID);
System.assertEquals(true, co_list[0].Owner.IsActive);
}
2) Run test class and check output in Developer Console:
### OUTPUT ##
WORK OWNER ISACTIVE : false
WORK OWNER ID : null
Workaround
If User fields are needed, run a separate SOQL query to retrieve them.
Reported By (21)
Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.