I have custom field in the Opportunity object that is a Lookup and maps the Opportunity object to the User object. I'm trying to write a query that retrieves fields from the Opportunity, and also includes the Email field from the User based on a WHERE clause of AccountId = 'somerandomaccountid'. Apologies if this is confusing, but can anyone help me out with understand cross-object queries with custom fields?

[SELECT Custom_Field__r.Email, ... FROM Opportunity WHERE AccountId = '..']

assuming the customfield is named Custom_Field__c

If the custom lookup field is named Foo__c and points at some object (standard or custom) the relationship name from object to its parent is Foo__r

If it is a standard object with a lookup to another standard object and the lookup field is named FooId, then the relationship name is Foo.

You can lookup the relationship names in any schema browser (such as Workbench) by going to the child object and inspecting the lookup field

| improve this answer | |
  • thanks! could you clarify this for me briefly? I am under the impression that fields/columns are separate from relationships – yen sid Mar 27 '19 at 23:02
  • @user66118 - updated answer; see SOQL guide for more details on relationship fields – cropredy Mar 27 '19 at 23:06

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.