Sub topic 3
Restricting lookup choices with lookup filters
This chapter assumes you can create a lookup relationship field in Object Manager and that you know basic SOQL and the difference between a parent and a child object. A lookup relationship lets a user pick any record of the target object by default. A lookup filter narrows that list.
What a lookup filter does
A lookup filter is a set of criteria attached to a lookup, master-detail, or hierarchical relationship field. When a user searches for or enters a related record, Salesforce checks each candidate against the criteria. Only records that pass can be chosen.
The filter works on the field you are editing, so it validates the child record. It does not change the parent records themselves.
A filter compares one of these against a value or another field:
- Fields on the target object, such as
Account.Industry. - Fields on the source object, the one that holds the lookup, such as
Contact.MailingCountry. - Fields on the current user, such as the user's profile or role.
- Fields on objects related to any of the above, reached through a relationship.
Required or optional
Each filter is either required or optional. The choice controls what happens when a user picks a record that fails the criteria.
| Setting | Behavior | Typical use |
|---|---|---|
| Required | The user cannot save a value that fails the criteria. | A business rule that must always hold. |
| Optional | The user sees a filtered list, but can choose to show all results. | A convenience that guides users. |
You can also write an error message for the required case. Make it say what the user should do, such as "Choose an active account in the Technology industry."
A worked example
Suppose a Project__c object has a lookup to Account, and only active accounts should be selectable.
- In Object Manager, open
Project__cand go to Fields & Relationships. - Open the Account lookup field and click Edit.
- In the Lookup Filter section, choose Account fields and set
Active__cequalsTrue. This assumes your org has a custom checkbox field namedActive__con Account. - Mark the filter as required, add an error message, and save.
Users now see only active accounts in the lookup dialog. Existing project records that point to an inactive account are not changed or deleted. The filter is applied when the field is next edited.
Behavior to know about
- Filters are checked on edit, not retroactively. Records saved before the filter existed stay valid until someone changes the lookup value.
- Required filters apply to data loaded through the API and to Apex DML. A failing insert returns a validation error, so test your data loads and triggers against the filter.
- A lookup filter on a master-detail field restricts which parent a child can be attached to. It does not remove the requirement that a parent exists.
- A field can hold only one lookup filter. Put all of the criteria into that one filter.
- Filters limit the criteria you can express. They cannot use formulas, so complex rules may need a validation rule or Apex.
Lookup filter or validation rule
Both can block a bad save. Use a lookup filter when you want users to see only valid choices up front. Use a validation rule when the logic needs formulas, multiple fields, or conditions on the source record that the filter cannot express. The two can be combined.
Exercise
A Contact lookup field has an optional lookup filter. A user needs a record that the filter hides. What can the user do, and how would you change the setup to stop this?
Show answer
The user can choose to show all results and select the hidden record. To stop it, change the filter from optional to required, and add an error message that explains the rule.
Exercise
You add a required lookup filter to a field that already has 5,000 records. Do those records fail immediately? Explain.
Show answer
No. Salesforce does not re-check existing values when you add the filter. A record is checked when its lookup value is edited, so older records that fail the criteria stay as they are until someone changes them.