Sub topic 17
Designing and auditing FLS with least privilege
This chapter assumes you can set field access in Setup, know what permission sets and permission set groups are, and can read basic SOQL. It covers how to design field access so that users get only what they need, and how to check the result.
Design principles
Least privilege means a user starts with no access to a field and receives only the Read or Edit access their job requires. In practice:
- Keep profiles minimal. Grant field access through permission sets, because a profile can't be partly removed from a user.
- Build one permission set per capability, such as "Account Financials Read" or "Account Notes Edit". Don't build one per person.
- Combine capabilities into permission set groups that match job roles. A role then gets a predictable bundle.
- Grant Read before Edit. Edit access always includes Read, so give Edit only to the users who change the data.
- Treat sensitive fields (revenue, health data, internal notes) as opt-in. Put each one in its own permission set so access is a deliberate choice.
Auditing access
Setup can answer "what can this permission set do?", but it is slow for the opposite question: "who can see this field?". The FieldPermissions object stores one row per field per permission set or profile. Query it to list every grantee of a sensitive field.
List<FieldPermissions> grants = [
SELECT Parent.Label, Parent.IsOwnedByProfile, Parent.Profile.Name,
PermissionsRead, PermissionsEdit
FROM FieldPermissions
WHERE Field = 'Account.Internal_Notes__c'
AND PermissionsRead = true
];
for (FieldPermissions fp : grants) {
String source = fp.Parent.IsOwnedByProfile
? 'Profile: ' + fp.Parent.Profile.Name
: 'Permission set: ' + fp.Parent.Label;
System.debug(source + ' | edit=' + fp.PermissionsEdit);
}Profiles appear in this object through a hidden permission set, which is why the code checks IsOwnedByProfile. Run the query in the Developer Console or an anonymous Apex window.
Review checklist
- List sensitive fields and confirm each has a named owner.
- For each field, run the audit query and check that every grantee is expected.
- Look for Edit grants where only Read is needed.
- Check that profiles do not grant more field access than the permission set groups intend.
- Confirm the permission sets in source control match the org. Deploy FLS as metadata so changes go through review.
- Keep an Apex test, like the
System.runAsexample from the testing chapter, for each critical field.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Granting Edit "just in case" | Users can change data they only needed to view | Grant Read, and add Edit only when required |
| One large permission set for all fields | Access can't be reduced for one group | Split by capability |
| Never reviewing after launch | Access accumulates as roles change | Schedule a recurring audit |
Exercise
A new custom field, CreditLimit_c, must be readable by Sales Managers and editable only by Finance. How would you set this up so that access stays auditable?
Show answer
Create two permission sets, "Credit Limit Read" (Read only) and "Credit Limit Edit" (Read and Edit). Add the Read set to the Sales Manager permission set group. Add the Edit set to the Finance permission set group. Leave profiles without access to the field. Then query FieldPermissions for Account.CreditLimit_c (or the object that holds it) to confirm only those two permission sets grant access.