2

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

  • i have the same error message: > SOQL statements cannot query more than 35 different parent types. And found exactly the same documentation: > 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. My custom object contains 39 lookups, ex the standard fields like CreatedById. The max is 40 lookups, so i didn't expect to receive the error message. As i can't find any more documentation, i agree with you VarunC, that a lookup is con – user25007 Sep 27 '15 at 10:52
5

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.

  • So if Contact had two different lookups but both to Account, let's say AccountB and AccountC, and we queried [SELECT AccountB.Name, AccountC.Name, Name FROM Contact]...would this count as one or two relationships? – Brian Mansfield May 20 '15 at 17:49
  • 1
    @BrianMansfield It would be two relationships. You're counting the number of relationship fields you use, not the number of objects involved (as my example, Account.Parent.Name, shows). – sfdcfox May 20 '15 at 17:54
  • 1
    interesting to read, and thnx for verbose reply. I've further tracked down the debugging to understand that I'm actually querying 33 Lookup fields (Custom added to object). This looks under the limit. so I was thinking does the Standard Lookup fields like OwnerId or CreatedById also calculate against this limit? One more finding I had was I'ma lso querying Formula fields, does this limit apply to Formula fields referencing other objects? – VarunC May 20 '15 at 19:39
  • @sfdcfox is there any way to catch this error? I've already placed the code inside Try/Catch but still unable to catch this error. – VarunC May 21 '15 at 12:14
  • I'm pretty sure cross object formulas count against the total limit, as they have to be part of the SQL that the system generates. Also, it's probably implemented as a governor limit, meaning you can't catch it. – sfdcfox May 21 '15 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.