0

So lets say objectA has a lookup of objectB which has a lookup of objectc Is there anyway of doing:

objectA.lookUpToB__r.lookupToC__r.fieldOfC__c ? or anything similar?

Im trying to avoid querying a map of B and C if possible.

objectA.lookupToB__c works just fine.

However, accessing a lookup inside of B To C doesnt seem to work.

1 Answer 1

2

Yes, and lookUpToB__r.lookupToC__r.fieldOfC__c is exactly what you would need to add to your query for object A

Something like

[SELECT Id, lookUpToB__r.lookupToC__r.fieldOfC__c
FROM ObjectA
WHERE Field__c = value]

You can do this up to 5 levels (B__r.C__r.D__r.E__r.F__r.FieldOnF__c), or down one level (a nested select, (SELECT Id, Field__c FROM ChildObjects__r))

The key, though, is that you can't just pull related data out of thin air. You need to include the data you want from related objects in a query. Records in trigger context variables (trigger.new and the like) also do not have any data from related objects in them (i.e. you need to query for related data, even in a trigger)

You also need to include the full path from the "Base" object (the one specified in the FROM clause) to the piece of related data for every field on a related object that you want to get data for.

Yes, that is quite tedious
...but queries are typically the governor limit you run into first when trying to scale code.

You must log in to answer this question.

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