Sub topic 5
Primary object and related objects
This chapter assumes you can build a basic report in Lightning Experience. It also assumes you know what a lookup and a master-detail relationship are, and that you have seen the report type picker in the report builder. Here you will learn how a report type picks one object as its anchor, and how related objects attach to it.
The primary object sets the anchor
Every report type has exactly one primary object. The primary object is the object whose records form the rows of the report. Related objects hang off it. They add columns and can filter which primary records appear, but they do not become the anchor.
The report type name usually tells you the primary object. In "Accounts with Contacts", Account is primary and Contact is related. In "Contacts & Accounts", Contact is primary and Account is related. The two types use the same two objects, but they answer different questions, and the row counts differ.
The primary object determines three things:
- The unit of a row. A report on Account counts accounts. A report on Contact counts contacts, even if every contact belongs to the same account.
- The folder category the type appears in.
- Which related objects you can add. You can only reach objects through relationships that start at the primary object.
How related objects attach
A related object joins the primary object through a relationship field. For a standard type, Salesforce has already chosen the relationships. For a custom report type, you choose them when you define the object chain.
A custom report type supports up to four levels of objects: the primary object (A) plus up to three related objects (B, C, D). Each object must be related to the one before it. A chain such as Account, Contact, Case works only if Contact relates to Account and Case relates to Contact.
Each link is a relationship you pick from the list that Salesforce offers for the previous object. Salesforce only lists relationships that exist. If an object does not appear in the list, no usable relationship exists from the object above it.
Lookup and master-detail in the chain
Both lookup and master-detail relationships can connect objects in a report type. The difference matters for what rows you get.
- Master-detail: a child record always has a parent, because the parent field is required. Joining from the parent to its children is usually straightforward, and a child never appears without a parent.
- Lookup: the parent field may be empty. A child can exist with no parent, and a parent can have no children.
Optional lookups are the reason the next chapter's topic, "A with B" versus "A with or without B", exists. The primary object chain defines which objects are connected. The join setting defines which records survive the connection.
What the join does to rows
When you add a related object, the report builds a row from the primary record and the related record together. This has two effects you should expect.
- Records can be filtered out. With an inner join ("A with B"), a primary record that has no related record is dropped.
- Rows can multiply. If an account has three contacts, the report holds three rows for that account, one for each contact.
The second effect surprises many people. Account-level fields repeat on every row for that account. If you sum an account field, such as Annual Revenue, across those rows, the total counts the account three times.
The two queries below show the same idea in SOQL, the Salesforce query language. The first returns only accounts that have contacts. The second returns all accounts, with their contacts nested under each one.
SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact)SELECT Id, Name, (SELECT Id, LastName FROM Contacts)
FROM AccountThe first query matches an "Accounts with Contacts" report type. The second matches "Accounts with or without Contacts". A report does not run SOQL you can see, but the behavior is the same.
Reading the primary object in a custom report type definition
The chapter's metadata example shows these ideas in a definition file. Look at the three elements that carry the structure.
<?xml version="1.0" encoding="UTF-8"?>
<ReportType xmlns="http://soap.sforce.com/2006/04/metadata">
<baseObject>Account</baseObject>
<category>accounts</category>
<deployed>true</deployed>
<description>Accounts with or without contacts</description>
<join>
<outerJoin>true</outerJoin>
<relationship>Contacts</relationship>
</join>
<label>Accounts with or without Contacts</label>
<sections>
<columns>
<checkedByDefault>true</checkedByDefault>
<field>Name</field>
<table>Account</table>
</columns>
<masterLabel>Accounts</masterLabel>
</sections>
<sections>
<columns>
<checkedByDefault>true</checkedByDefault>
<field>LastName</field>
<table>Account.Contacts</table>
</columns>
<masterLabel>Contacts</masterLabel>
</sections>
</ReportType>baseObjectis the primary object, here Account.joindescribes one related object.relationshipnames the relationship from the object above, hereContacts, which is the child relationship name on Account.outerJoinset totruemeans "with or without".- Each
sectionsblock is a group of fields in the layout. Thetablevalue shows the path from the primary object.Accountis the primary object itself.Account.Contactsis the Contact records reached through that relationship.
For a deeper chain, a join element nests inside another join. The table path grows with each level, for example Account.Contacts.Cases.
Choosing the primary object
Pick the primary object by asking what one row of the report should represent.
| Question you want to answer | Row represents | Primary object |
|---|---|---|
| How many contacts do we have by region? | A contact | Contact |
| Which accounts have no open cases? | An account | Account |
| What is the total value of opportunities by product? | An opportunity product line | Opportunity |
| Which cases came from which contacts? | A case | Case |
If the answer would be a list of one kind of thing, that thing is the primary object. When two choices seem equal, choose the one whose count you want to report. You can still show fields from the other object.
Common errors
- Starting from the wrong object. If you want accounts that have no contacts, an Account-primary type with "with or without" works. A Contact-primary type cannot show an account that has no contacts, because no contact row exists to carry it.
- Double counting. Summing a parent-level field across a report with child rows inflates the total. Group by the parent, or use a summary on the child object instead.
- Expecting a missing relationship. If the object you need is absent from the list at a level, it is not related to the object above. Reorder the chain, or choose a different primary object.
- Exceeding the four-object limit. A chain longer than four objects does not fit in one report type. Split the question into two reports, or restructure the data.
Exercise
A report type has Account as the primary object and Contact as a related object, using an inner join. Account "Acme" has 4 contacts. Account "Zenith" has none. How many rows does the report show for these two accounts, and what changes if you switch to "A with or without B"?
Show answer
With the inner join, the report shows 4 rows, all for Acme. Zenith is dropped because it has no contact. With "A with or without B", the report shows 5 rows: the same 4 for Acme plus 1 for Zenith, with its contact columns blank.
Exercise
You want a list of contacts, including those whose Account field is empty. Which object should be primary, and why can't an Account-primary type produce this list?
Show answer
Contact should be primary. Each row of the report is a contact, so a contact with no account still gets a row (with a with-or-without join to Account). An Account-primary type builds rows from accounts, so a contact that has no account is never reached.