You need to sign in to do that
Don't have an account?
bharath kumar 52
How to access parent above 5 levels in a SOQL query?
Hi All,
How can i access a parent record more than 5 levels above in a SOQL query?
Thanks and Regards,
Bharath Kumar M
'In each specified relationship, no more than five levels can be specified in a child-to-parent relationship. For example, Contact.Account.Owner.FirstName (three levels).'
This is in the first table under 'Reltionship Queries'
Hope this helps!
AM
In each specified relationship, only one level of parent-to-child relationship can be specified in a query.
This is parent-child Query upto 4 levels
public List<Account> grtaccounts(){
List<Account> lstacc = [Select Id,Name,(Select Id,LastName From Contacts),(Select Id,CloseDate,Amount,StageName,Name from Opportunities),(Select Id,Status,Origin,AccountId,ContactId from Cases) From Account];
return lstacc;
}
Thanks!
Hope this helps! Please Mark It As a Best Answe
Take a hierarchy use case for example.
From there you can use nodeIdsWithHigherParents in the WHERE clause of another query to get 5 more levels.
You'll need to write some logic to combine the two levels depending on your needs.
This is only possible if you are able to create appropriate cross-object formula fields that you use in your query.
I could, for example, create these two cross-object formula fields in order to access the name of great-great-great-great-great-great-great-grandparent Account for an account:
- Field: "G-G-G Grandparent Name":
- API: G_G_G_Grandparent_Name__c
- Type: Formula (Text)
- Formula: Parent.Parent.Parent.Parent.Parent.Name
- Field: "G-G-G-G-G-G-G Grandparent Name":
- API: G_G_G_G_G_G_G_Grandparent_Name__c
- Type: Formula (Text)
- Formula: Parent.Parent.Parent.Parent.G_G_G_Grandparent_Name__c
This second field effectively allows me to query "Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Name" if I query it for a given Account. Deeper paths are also possible, I believe, with such chaining.