Sub topic 12
Choosing the right keyword for each kind of class
This chapter assumes you know basic Apex syntax, how organization-wide defaults, roles, and sharing rules control record access, and what profiles and permission sets do. It also assumes you have read the earlier chapters on with sharing, without sharing, and inherited sharing.
Start from the default, then justify exceptions
A sound rule is to pick the most restrictive keyword that still lets the class do its job. Use with sharing unless you can name a specific reason to do otherwise. Use without sharing only for a narrow task that must see records the user cannot, and keep that code small and separate. Never leave the keyword off, because a class with no keyword gets its mode from how it is called, which is hard to predict and hard to review.
| Kind of class | Suggested keyword | Reason |
|---|---|---|
| Controller (Lightning, Visualforce, REST entry point) | `with sharing` | It is the entry point for a user request, so it should respect that user's records. |
| Service or business logic | `with sharing` | Logic should follow the user's access by default. |
| Data-access or utility class used by many callers | `inherited sharing` | The caller decides, and an entry point runs `with sharing`. |
| Narrow system task (for example, a roll-up that needs all records) | `without sharing` | Visibility is needed on purpose, so isolate it. |
Controllers and services: with sharing
Controllers receive requests from users, so they should enforce what those users can see. Services called from controllers should do the same, so the rule holds even if another entry point calls them later.
public with sharing class OrderController {
public static List<Account> getMyAccounts() {
return AccountRepository.findByName('Acme');
}
}Data-access classes: inherited sharing
A repository class that runs queries for many callers should not choose a mode for them. inherited sharing lets the caller decide. If the repository is called from the with sharing controller above, it enforces sharing. If it is the entry point of a transaction, it runs with sharing as well, which is a safe default.
public inherited sharing class AccountRepository {
public static List<Account> findByName(String name) {
return [SELECT Id, Name FROM Account WHERE Name = :name];
}
}System tasks: a small without sharing class
When one operation really needs all records, put only that operation in its own without sharing class. Do not make a whole service without sharing to fix one method. Each method runs under the mode of the class that defines it, so the exception stays limited to that class.
public without sharing class CaseCounter {
public static Integer countAll() {
return [SELECT COUNT() FROM Case];
}
}Return only what the caller needs, such as a count, instead of the raw records. This keeps the widened access from leaking through.
Add user mode when permissions matter
The sharing keyword limits records. It does not enforce object permissions or field-level security. For a class that reads or writes data on behalf of a user, add user mode as well.
public with sharing class ContactService {
public static List<Contact> getContacts() {
return [SELECT Id, LastName, Email FROM Contact WITH USER_MODE];
}
}A quick decision flow
Common mistakes
- Marking a controller
without sharingbecause a test failed under a limited user. - Leaving the keyword off a helper class and assuming it behaves like its caller.
- Assuming
with sharingalso enforces field-level security.
Exercise
You are writing a class that queries Opportunity records. It will be called from a with sharing Lightning controller and from a scheduled batch job that is its own transaction entry point. You do not want the class to force a mode on either caller. Which keyword should you use, and what mode does it run in for each caller?
Show answer
Use inherited sharing. Called from the with sharing controller, it runs with sharing. As the entry point of the scheduled job's transaction, it also runs with sharing, which is the safe default for this keyword.
Exercise
A with sharing service needs one method that counts every Case in the org, including ones the user cannot see. How do you do this with the smallest widening of access?
Show answer
Put the count in a separate without sharing class, such as CaseCounter, and call it from the service. Return only the number, not the records.