How to query multiple levels of lookup relationships? #102
-
|
I've been able to find lots of examples of how to query for fields on a related object to the one being queried for (e.g. Account Name when querying on Opportunity) but not sure how to go multiple levels. For example: Has anyone accomplished this before? Or do I need to run a separate query on Account to go that extra level? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Hi @maxner-sdocs , Great question! I would suggest https://soql.beyondthecloud.dev/api/soql#with-related-field1---field5 List<Opportunity> opps = SOQL.of(Opportunity.SObjectType)
.with(Opportunity.Name)
.with(Opportunity.Account.getDescribe().getRelationshipName(), Account.Name)
.with('Account.CreatedBy', User.Name)
.toList();Why in this way? SOQL Lib use relationships name as a String, and fields as SObjectField so at least target fields are stored as reference. SOQL Lib will do something like that: |
Beta Was this translation helpful? Give feedback.
Hi @maxner-sdocs ,
Great question!
I would suggest https://soql.beyondthecloud.dev/api/soql#with-related-field1---field5
Why in this way?
It's impossible to get multiple levels of relationship with
getRelationshipName().SOQL Lib use relationships name as a String, and fields as SObjectField so at least target fields are stored as reference.
Relationship Name: 'Account.CreatedBy',
Target Field: User.Name
SOQL Lib will do something like that:
Relationship Name + SObject…