You are right.Subquery can go only one level deep. If I understand your object model correctly, you can get everything using two subqueries in one query like below,
SELECT Id, Name,
(SELECT Contact_Name__r.FirstName, Contact_Name__r.LastName
FROM Activity_Lists__r LIMIT 50000),
(SELECT Id, Contact_Name__r.FirstName, Contact_Name__r.LastName
FROM Activity_List_Contacts__r)
FROM Activity_List__c
WHERE Campaign__c IN :subCampaignIds
OR Campaign__c = :CampaignId
LIMIT 50000
First subquery will return child activity lists, second subquery will return Activity List contacts records from child relationship.
Every time you go through a relationship to a parent, each unique relationship you reference counts as a relationship for the purposes of this limitation.
First, let's take a look at zero relationships:
SELECT AccountId, Name FROM Contact
Since there's no . notation, there's no parent relationships being specified, so the total number of relationships is zero. You can always get all of the ID values for all of the reference fields on a record at once.
So, what is this limitation talking about? It refers to using the . notation to gain access to parent data directly:
SELECT Account.Name, Account.AccountNumber, Name FROM Contact
In this case, we're using 1 relationship (Account) to get two pieces of data from the account plus some data from the contact.
Let's say we also wanted to get the account's parent account data. We can do this, too:
SELECT Account.Parent.Name, Name FROM Contact
However, we have had to walk through two relationships to get there (of the allowed 35). Basically, salesforce is telling us that we're allowed to use up to 35 joins in the SQL that will be built from our SOQL query.
Of course, there's other limitations that exist, too, such as the maximum number of parents you can reference in a chain, but aren't relevant to this particular limit.
If you get this error, you need to break up your query into multiple pieces. For example, query all of the fields on the record, then aggregate the ID values together and perform queries on the parent data, and repeat as necessary.
Best Answer
The error message describes the issue pretty well.
The "root entity" is the object that you're using in the
FROM
clause of the outermost query (Tower__c
in your case), and each time you query from a parent record to a child record (or from a child record to a parent record), that counts as one "level" away.You can only query one level deep from parent -> child.
Tower__c
->Floor__c
is one parent -> child levelTower__c
->Floor__c
->Unit__c
is two parent -> child levels (specifically,Unit__c
is two levels away fromTower__c
, and is thus invalid in a query starting fromTower__c
).Your options here are to split this into two separate queries, or to adjust your query to start from
Floor__c
instead ofTower__c
.Splitting into two queries:
Changing the root object of your query: