1

I have a custom object called Sport, and sport has an Owner like every other object. When I do a Query on Sport:

Map SportMap=new Map([SELECT Id, OwnerId, Owner.Email FROM Sport__C Limit 50000]);

It works just fine, but as soon as I change Owner.Email to Owner.NewRole__c which is a custom field on the user record, it gives me an error message: No such column 'NewRole__c' on entity 'Name'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

Why is that and how can I fix it? Thanks!

3

The Owner field is likely a polymorphic relationship field. This means the relationship can point to more than one object (i.e. User and Queue). If you happen to have the Polymorphism feature enabled in your org, you can use the TYPEOF operator like this:

SELECT ID,
  TYPEOF Owner
    WHEN User THEN NewRole__c
  END
FROM Sport__c 
LIMIT 50000

Otherwise, you'll need to query out the users separately and perform a secondary query to get the related user data.

More info can be found here. You can also request to be included in the polymorphism beta by submitting a case to Salesforce.

  • Awesome help Kevin, I didn't know about TYPEOF, I just submitted a case to Salesforce for them to enable this feature. Let's say this works, and I get a List of Sports__c, how do I access the NewRole__c if my list is a list of Sports? – Leo Oct 8 '15 at 19:01
  • I believe you can access it with regular dot notation. I'm not sure though. Let us know! – Kevin O'Hara Oct 13 '15 at 12:07
  • 1
    Well since TYPEOF is not ready for Production and that is where I need the code to go, I just created a Set<Id> with those IDs and then I created a Map of Users. That worked and I was able to deploy my code. Thanks for your help! – Leo Oct 14 '15 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.