Sub topic 13
Limits and common mistakes
This chapter assumes you can write child-to-parent queries with dot notation and parent-to-child subqueries. It also assumes you can read the results in Apex. It collects the limits that apply to relationship queries and the errors that come up most often.
Choose the direction first
Most mistakes start with picking the wrong direction. Decide which object you want one row per record of. That object goes in the FROM clause.
Limits to know
These limits apply to relationship queries. Salesforce adjusts some of them between releases, so check the current SOQL documentation before you rely on an exact number.
- A child-to-parent path can go up at most five levels, such as
Contact.Account.Owner.Manager.Name. - A query can contain at most 55 child-to-parent relationships and 20 parent-to-child subqueries.
- Child rows returned by subqueries count toward the 50,000-row total for the transaction.
- Each query counts toward the per-transaction SOQL limit of 100 synchronous or 200 asynchronous queries.
- A semi-join or anti-join subquery must select an ID or lookup field. It cannot use
ORDER BYorLIMIT.
The most common way to hit the query limit is to run a query inside a loop. A subquery fetches the same data in one query:
// Wrong: one query per account, can exhaust the query limit
for (Account a : [SELECT Id FROM Account LIMIT 200]) {
List<Contact> cs = [SELECT Id FROM Contact WHERE AccountId = :a.Id];
}
// Right: one query returns accounts and their contacts
for (Account a : [SELECT Id, (SELECT Id FROM Contacts) FROM Account LIMIT 200]) {
Integer n = a.Contacts.size();
}Naming mistakes
Field names and relationship names are different things. A lookup field holds an ID. The relationship name lets you reach the record that ID points to.
| Mistake | Fix |
|---|---|
| `Project__c.Name` for a custom lookup | Use the relationship name: `Project__r.Name` |
| `FROM Account (SELECT ... FROM Contact)` | Use the child relationship name: `FROM Contacts` |
| `Account.Name` on a custom object with a lookup called `Project__c` | The path uses the relationship name `Project__r`, not the object name |
| `Tasks` for a custom child | Check the relationship name on the lookup field. It is often `Tasks__r` |
| `Account` where you need the ID | `Account` is the relationship and `AccountId` is the field |
The child relationship name in a subquery is plural for standard objects, such as Contacts. For custom relationships it is whatever you set on the field, ending in __r.
Subquery behavior that surprises people
LIMIT inside a subquery applies to each parent, not to the whole result. This query returns up to five contacts for every account:
SELECT Id, Name,
(SELECT Id, LastName FROM Contacts ORDER BY LastName LIMIT 5)
FROM Account
WHERE Industry = 'Technology'The outer WHERE filters accounts only. To filter the contacts, put a WHERE inside the subquery. To keep only accounts that have matching contacts, add a semi-join.
Apex mistakes
- A parent lookup can be empty.
c.Account.Namethrows a null-reference error whenAccountIdis null. Usec.Account?.Name. - A subquery on a parent with no children returns an empty list, not null. You can call
.size()safely. - You can only read fields you selected. Reading
c.Account.Phonewhen you selected onlyAccount.Namethrows aSystem.SObjectException. - Using the field name instead of the relationship name in code fails the same way. Read
c.Accountfor the record andc.AccountIdfor the ID.
Checklist before you run a query
- Does the FROM object match the row you want?
- Did you use
__rfor custom relationships and the exact child relationship name in subqueries? - Did you select every field you read in Apex?
- Can the parent be null, and does your code handle that?
- Is the query outside any loop?
Exercise
A developer writes SELECT Id, Name, Project__c.Name FROM Task__c and the query fails. What is wrong, and what is the corrected query?
Show answer
Project__c is the lookup field, which holds an ID. To read fields from the parent record you must use the relationship name Project__r. The corrected query is SELECT Id, Name, Project__r.Name FROM Task__c.
Exercise
An Apex loop runs [SELECT Id FROM Contact WHERE AccountId = :a.Id] once for each of 150 accounts. Which limit is at risk, and how do you fix it?
Show answer
The per-transaction SOQL query limit is at risk. Query the accounts once with a subquery, (SELECT Id FROM Contacts), and read a.Contacts in the loop.