0

Let's say I have Lookup between custom object and standard Account object as a master. But

select id, name, Account.id from MyObect__c

always returns an error

'Didn't understand relationship Account in field path' I'm 100% sure that there is the field with lookup to Account in MyObject. What am I doing wrong and can I resolve this issue?

2

If you're dealing with a custom SObject, then that relationship is going to be a custom field.

Therefore, it'll be Account__c, and you'll need to use Account__r.Name1 in your query. That'll be true for both lookup relationships and master-detail relationships.

The only time you can avoid using __c or __r is when you're dealing with a standard relationship (e.g. OpportunityId between OpportunityLineItem and Opportunity)

1: If you're just looking for the Id, there's no reason to query up the relationship hierarchy. Just query for the relationship field on your custom object (i.e. Account__c). If you do need some other piece of data on the related object, then you'll need to use __r to access it in the query

5
  • I'm afraid I don't quiet understand your answer. I have 2 objects - standard Account object and custom MyObject. But according to your suggestions select Account__r.name doesn't work for me as well – pincet Oct 18 '20 at 19:37
  • 1
    @pincet If your relationship is a custom field then, whatever you've called it, the API name ends in __c. You need to change that to __r when you're trying to query up from a child record to get parent record data. If that's still not enough, then you should edit your question to contain more details. – Derek F Oct 18 '20 at 19:45
  • Parent is the standard Account object and I need to retrieve fields using child-to-parent query – pincet Oct 18 '20 at 20:00
  • 1
    @pincet and what is the API name of your relationship field to Account? – Derek F Oct 18 '20 at 20:32
  • Derek, it was my bad. Now it's clear. Thank you – pincet Oct 19 '20 at 8:07
0

It seems this is a custom object.

[SELECT name, Account__c FROM MyObect__c]

Like Derek said before, navigate to your object and see the api name for it and add the __c to it if you are trying to get the reference for the account.

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.