[SalesForce] What does this error mean – SOQL statements cannot query more than 35 different parent types

From the Spring 15 Docs, I found this info under Relationship Query Limits:

No more than 35 child-to-parent relationships can be specified in a
query. A custom object allows up to 25 relationships, so you can
reference all the child-to-parent relationships for a custom object in
one query.

I am trying to run a query for Child Relationship objects of a CustomObject. Now I think it means that I cannot run query with 35 different lookup fields? I tried finding more details about it but I could not make clear sense of what the documentation want me to read.

When I open the Custom Object the "Limits" section reads that a Maximum of 40 "Custom Relationship Fields" can be created on that object. But then by the documentation it also means that I can only query 35 of the fields, and there are System defined Lookup fields as well in the object which I'm not counting here?

I'm really confused here how to overcome this since I could not clearly understand the meaning of the error. Has someone else here also experienced this kind of error in SOQL?

Source Documentation Link: here

Best Answer

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.

Related Topic