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
akallioWileyakallioWiley 

Invalid Field Error for Custom Fields but Not Standard Fields on User object?

In the code below the if statement is checking for a value that is using the OwnerId field on a Custom object to reference a field on the User object. When I use a custom field in the statement, I get the following error message: Invalid field District__c for SObject Name

Here is the code that generated that error:
 
if(contactEval.Owner.District__c == '') {
								approvalReasons.add(BUDGET_LIMIT_EXCEEDED_REASON);
                                itemApprovalReasons.add(BUDGET_LIMIT_EXCEEDED_REASON);
                                requiresProductApproval = true;
                            }

But, if I use a Standard Field, I get no error message. For example: 
 
if(contactEval.Owner.IsActive) {
								approvalReasons.add(BUDGET_LIMIT_EXCEEDED_REASON);
                                itemApprovalReasons.add(BUDGET_LIMIT_EXCEEDED_REASON);
                                requiresProductApproval = true;
                            }

I have tried this for a few different Standard and Custom fields to confirm that it's not these specific fields. 

Any ideas why I can reference a custom field on the User object?
akallioWileyakallioWiley
Ok. I just figured out the reason why, I think. This must be happening because the owner field on that object can either reference a User or Queue. In other words, the OwnerId field is polymorphic. 


Any tips on how to do that would appreciated. 
BalajiRanganathanBalajiRanganathan
I believe you need to issue a SOQL to get the User record.
Owner.Type will tell you if it is Queue or User. Also all User IDs start with '005' and all Queue IDs start with '00G'.
 
Set<id> ownerIds = new Set<id>();

for (ContactEval l : Trigger.new) {
  ownerIds.add(l.OwnerId);
}

Map<id, User> owners = new Map<id, User>([Select Id,District__c from User Where Id in :ownerIds]);