Sub topic 9
Controlling access to objects and fields
This chapter assumes you know the Salesforce Setup menu, can create a custom object such as Invoice__c, and understand the difference between an object and a field. It covers how Salesforce decides who can see and change data. Standard and custom objects follow the same rules.
Two layers of access
Salesforce checks access in two layers.
- Object and field permissions decide whether a user can work with an object or field at all. These are set with profiles and permission sets.
- Record access decides which individual records a user can see. This is set with organization-wide defaults (OWD), role hierarchy, sharing rules and manual sharing.
A user needs both layers to pass. Read permission on Invoice__c does not show any records if the sharing model hides them. A wide-open sharing model does not help a user who has no object permission.
Object permissions
Each object has up to six permissions: Read, Create, Edit, Delete, View All and Modify All. The first four are the usual CRUD set. View All and Modify All ignore sharing for that object, so grant them sparingly.
Field-level security
Field-level security (FLS) controls each field separately, with two settings: Read and Edit. If a user lacks Read on Amount__c, the field disappears from layouts, reports and list views. Hiding a field from a page layout is not security. The data still shows in reports and API calls unless FLS blocks it.
Profiles and permission sets
A profile is the base set of permissions assigned to a user. Each user has exactly one profile. A permission set adds permissions on top of the profile. A user can have many permission sets, and a permission set can be grouped with others in a permission set group.
Use a minimal profile and grant access through permission sets. Then you can give one team access to Invoice__c without creating another profile.
| Item | Per user | Can grant | Can remove |
|---|---|---|---|
| Profile | Exactly one | Yes | Yes (current approach) |
| Permission set | Many | Yes | No, it only adds |
Because permission sets only add, the user gets the union of the profile and all assigned permission sets.
Record access with sharing settings
The OWD setting for each object sets the most restrictive level of record access: Private, Public Read Only, or Public Read/Write. Other tools then open access up:
- Role hierarchy gives managers access to their reports' records.
- Sharing rules share records with groups based on owner or field criteria.
- Manual sharing lets an owner share a single record.
The metadata for Invoice__c earlier in this tutorial includes <sharingModel>ReadWrite</sharingModel>. That sets the OWD to Public Read/Write. A table of invoices that holds financial data would often use Private instead.
Master-detail children have no OWD of their own. They inherit the sharing of the master record. A lookup child has its own OWD.
Security in Apex
Apex runs in system mode by default, so it ignores object and field permissions. Respect them explicitly. This query fails with an error if the user lacks access to the fields:
List<Invoice__c> invoices = [
SELECT Name, Amount__c
FROM Invoice__c
WHERE Status__c = 'Open'
WITH USER_MODE
];
System.debug(invoices.size());WITH USER_MODE enforces object permissions, FLS and sharing for the running user. You can also check one field directly:
Boolean canRead = Invoice__c.Amount__c.getDescribe().isAccessible();
System.debug('Can read Amount__c: ' + canRead);Common errors
- Adding a field and forgetting FLS. New custom fields are not visible to non-admin users until a profile or permission set grants access.
- Treating layouts as security.
- Giving Modify All to fix a sharing problem. Use a sharing rule instead.
- Testing only as an admin. Use "Login as" or run tests with
System.runAs.
Exercise
A sales user has Read on Invoice_c through a permission set, and the Invoice_c OWD is Private. The user opens the Invoices tab and sees only their own invoices. Which layer causes this, and what is one way to let the user see a team's invoices without granting View All?
Show answer
The record access layer causes it, because the Private OWD hides records the user does not own. The object permission is fine. Add a sharing rule that shares the team's invoices with the user's group or role. Role hierarchy access for a manager would also work.