0

Good morning,

In the custom object Equipment_maintenance_item__c there is a lastModifiedById lookup field to the User object.

My understanding is that User is the parent object, and that to query it from the child my query should be like this:

Select createdbyId, User__r.id from Equipment_maintenance_item__c

However this does not work, and I cannot see why since the following query between two custom objects does work:

Select createdbyId, Equipment__r.id from Equipment_maintenance_item__c

I have been checking the documentation and other questions on the stack exchange but I cannot see what I'm missing, could anybody give me a hand?

Many thanks!

4
  • 1
    Select createdbyId, createdby.id from Equipment_maintenance_item__c
    – kurunve
    Dec 30, 2020 at 11:02
  • Thanks a lot!! I had no idea the Lookup field name replaces the name of the object!
    – Sergi
    Dec 30, 2020 at 11:12
  • 1
    @kurunve pls create an answer for this, otherwise the question stays open forever (or someone else might post an answer and you won't get the credits ;-) ) Dec 30, 2020 at 11:17
  • I don't fully understand by we have to remove the 'id' bit in the field name and use the dot notation to access the fields, could anybody point me to a resource on this? I haven't seen anything like this in any of the books I have, thanks a lot!
    – Sergi
    Dec 30, 2020 at 11:34

1 Answer 1

4

SOQL does not refer to the Object API directly. If you have a Look of User object with named Created_By__c then you have to use the given field API name only in your SOQL query.

SELECT Id, Created_By__c, Created_By__r.Name,.. FROM Equipment_maintenance_item__c

Same goes for standard fields as well.

SELECT Id, CreatedById, CreatedBy.Name FROM Equipment_maintenance_item__c

to get the Name or any other Field.

For any Standard fields, you don't append __r to get related fields. But if you have any Custom Fields (even if referring to Standard Object you must use __r and actual field name. (Example 1).

1
  • 1
    Thanks a lot, much clearer now!
    – Sergi
    Dec 30, 2020 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .