Sub topic 2
Lookup Relationships: Loose, Optional Links
This chapter assumes you know standard and custom objects, fields, and records, can navigate Setup and the Object Manager, and can read a basic SOQL SELECT. A lookup relationship is the simpler of the two main relationship types. It links one record to another without making the two records depend on each other.
What a lookup relationship is
A lookup relationship is a relationship field on a child object that points to a parent record. The parent can be a standard or custom object. The field stores the Id of the parent record.
Salesforce already uses lookups. The AccountId field on Contact is a lookup to Account. Opportunity, Case, and many other standard objects use the same pattern.
A lookup is loosely coupled. The child record keeps its own identity and settings:
- The lookup field can be empty, unless you make it required.
- The child has its own owner and its own sharing. It does not inherit them from the parent.
- Deleting the parent does not delete the child by default.
- The lookup field cannot be the source of a native roll-up summary field.
Creating a lookup
You create a lookup in Setup, using the Object Manager.
- Open the child object in Object Manager and choose Fields & Relationships.
- Click New and choose Lookup Relationship, or choose Master-Detail Relationship if you need that type instead.
- Select the parent object in the Related To list.
- Enter the field label. The field name and the child relationship name are filled in for you. You can edit them.
- Choose what happens when the parent is deleted (covered below).
- Set field-level security and page layouts, then save.
The child relationship name matters for queries. For a custom lookup, the query uses it with the __r suffix, as in Enrollments__r.
Optional or required
By default a lookup is optional. Users can save a child record with no parent. If a business rule needs a parent, you can mark the field required. Salesforce then treats it as a required field, but the relationship is still a lookup, with the delete behavior described next.
What happens when the parent is deleted
When you create the lookup, you choose one of these options for deletion of the parent:
| Option | Behavior | Notes |
|---|---|---|
| Clear the value of this field | The child stays and its lookup field becomes empty. | Default. Not available if the field is required. |
| Don't allow deletion of the lookup record that's part of a lookup relationship | The parent cannot be deleted while children point to it. | Protects parents that are still in use. |
| Delete this record also | Deleting the parent also deletes its children. | Not available for every parent object. |
Pick the option that matches the business meaning of the link. If a Project can disappear and its Tasks should remain, clear the value. If a Course must not vanish while Enrollments reference it, block the deletion.
Reading across a lookup
You can read and filter on parent fields from the child by using dot notation on the relationship name.
SELECT Id, LastName, Account.Name
FROM Contact
WHERE Account.Industry = 'Technology'Here Account is the relationship name, so Account.Name and Account.Industry are fields on the parent. Contacts with no account have a null Account.Name. A filter on a parent field leaves them out of the results.
Common errors
- Expecting inherited access. Lookup children do not inherit the parent's owner or sharing. A user who can see an Account may still be unable to see a related record, and the reverse is also true.
- Expecting a roll-up summary. A lookup cannot have a native roll-up summary field. To total or count child records, use another approach, such as an aggregate query:
Set<Id> accountIds = new Set<Id>{ '001000000000001AAA' };
List<AggregateResult> totals = [
SELECT AccountId, COUNT(Id) total
FROM Contact
WHERE AccountId IN :accountIds
GROUP BY AccountId
];The Id above is a placeholder. Replace it with a real Account Id in your org.
- Forgetting null parents. Code that reads
contact.Account.Namefails when the lookup is empty. Check for null first. - Choosing the wrong delete option. The default clears the field silently, which can leave orphaned child records that nobody notices.
Exercise
A Task-like custom object has a lookup to Project__c. Users must be able to keep tasks after a project is deleted, and some tasks have no project at all. Which delete option and required setting do you choose, and why?
Show answer
Leave the field optional and choose "Clear the value of this field." The field can then be empty, so tasks without a project are valid. When a project is deleted, its tasks stay and their lookup becomes empty. A required field would conflict with both needs, and it would also rule out the clear option.