This seems to be very strange.
While we can use Owner
relationship explicitly for many standard objects like Account
and run this code without problems
Account a = [ select Owner.Id from Account limit 1 ];
User u = a.Owner;
while we try to execute the same code for Case
Case a = [ select Owner.Id from Case limit 1 ];
User u = a.Owner;
or for custom object
CustomObject__c a = [ select Owner.Id from CustomObject__c limit 1 ];
User u = (User)(SObject) a.Owner;
then we receive the same error
Line: 2, Column: 1
System.TypeException: Invalid conversion from runtime type Name to User
Is there a workaround or fix for this problem?
Note that CustomObject__c
is not a detail in master-detail relationship and it has OwnerId
field and implicit Owner
relationship which can be either Queue
or User
so if we comment the second line the code doesn't fail. Also Case
is not a detail in master-detail relationship and it has OwnerId
field and implicit Owner
relationship which can be either Queue
or User
so if we comment the second line the code doesn't fail.