Sub topic 5
Primary object and related objects
This chapter assumes you can build a basic report in Lightning Experience and know how lookup and master-detail relationships link objects. It also assumes you have seen standard report types such as "Accounts" or "Opportunities with Products".
The primary object sets the scope
Every report type has one primary object. The primary object decides which records the report starts from. Each row in the report comes from one primary record, and related records attach to that row.
The choice affects three things:
- Which records can appear. A report on Accounts as the primary object starts from accounts. A contact that has no account cannot appear in it.
- Which fields you can use. You can only pick fields from the primary object and the related objects in the type.
- Which record each row represents. Row counts and summaries follow the primary object.
When you save a custom report type, you cannot change its primary object. If you choose wrong, you need to create a new type.
Related objects form a chain
Related objects are the objects you add to the primary object. Each one must be reachable through a lookup or master-detail relationship from the object before it.
A custom report type can include the primary object plus up to three related objects, so four objects in total. They form a chain. Each related object connects to the one above it, not necessarily to the primary object.
In this chain, Opportunity Products can only be added after Opportunities, because the relationship runs from opportunity to its products. You cannot skip a level.
Reading the direction of a relationship
The same two objects can serve in either order, and the order changes the report. Compare these two queries. They show what each primary object gives you.
SELECT Id, Name, (SELECT Id, LastName FROM Contacts)
FROM AccountSELECT Id, LastName, Account.Name
FROM ContactThe first starts from accounts and nests the contacts under each one. The second starts from contacts and reads one parent field. A report type with Contact as primary produces one row per contact and cannot list accounts that have no contacts.
Use this rule of thumb: pick as primary the object whose records you want to count or list.
Standard types follow the same pattern
Standard report types are built the same way. The name tells you the chain. "Accounts with Contacts" has Account as primary and Contact as related. "Opportunities with Products" has Opportunity as primary and its products as related.
The related object is the one that can multiply rows. An account with three contacts gives three rows in a report built on that chain. Keep this in mind when you add sums, because account-level numbers repeat on each row.
Choosing the chain for a new type
- Name the question the report answers, such as "Which opportunities have products from a given family?"
- Choose the object whose records you list as the primary object.
- Add related objects only as far as the question requires. Each extra level adds fields to scroll through and can multiply rows.
- Confirm that each link in the chain is a lookup or master-detail relationship. If an object has no such link, it cannot be added.
Whether each related object is required or optional is the subject of the next chapter.
Exercise
A manager wants a report that lists every case, with the name of the account it belongs to. Which object should be primary, and why?
Show answer
Case should be primary, because each row should be one case. Account is added as the related object (or its fields are read through the case's account lookup). If Account were primary, the rows would be accounts, and accounts without cases would not give you case-level rows.