Sub topic 12
When system mode is deliberate, and its pitfalls
This chapter assumes you know how WITH USER_MODE, as user, and AccessLevel.USER_MODE work, and the difference between sharing rules and CRUD/FLS. User mode should be your default. A few jobs still need system mode, and this chapter shows how to use it deliberately and keep it small.
Legitimate reasons to use system mode
System mode is correct when the code does work the user is not meant to control or see directly. Common cases:
- Audit and log records. Users should not need create access to an
Audit_Log__cobject just because they edited an account. - Aggregates over hidden data. A service that computes a count or total across records the user cannot see, and returns only the number.
- System-owned fields. Setting an integration status or a computed score that no profile should be able to edit.
- Cross-user housekeeping. Reassigning records or sending notifications to users who have different access.
In each case the user's action is allowed, but a side effect of it needs more access than the user has.
Scope it tightly
Do not switch a whole class to system mode. Isolate the privileged operation in one small method, or one inner class, and say why in a comment.
public with sharing class AccountService {
public static void renameAccount(Id accountId, String newName) {
Account acc = new Account(Id = accountId, Name = newName);
update as user acc; // the user's own change: user mode
AuditLogger.record(accountId, 'Account renamed');
}
}public with sharing class AuditLogger {
// System mode on purpose: users have no access to Audit_Log__c,
// but every change must be logged.
public static void record(Id recordId, String message) {
Audit_Log__c entry = new Audit_Log__c(
Record_Id__c = String.valueOf(recordId),
Message__c = message
);
Database.insert(entry, AccessLevel.SYSTEM_MODE);
}
}The user's update runs in user mode. Only the log write runs in system mode. AccessLevel.SYSTEM_MODE makes the choice visible in the code, which is better than relying on a missing keyword.
Pitfalls
Sharing keywords still matter in system mode
System mode ignores object permissions and FLS. Record visibility is still decided by the class's sharing keyword. A with sharing class in system mode skips CRUD and FLS but still respects sharing rules. A without sharing class skips both. Choose the keyword on purpose.
Classes with no keyword
A class with no sharing keyword inherits sharing from its caller. When it is the entry point, such as an Aura or LWC controller method, it behaves as without sharing. Always declare with sharing, without sharing, or inherited sharing explicitly.
Privileged code that returns data
The most serious mistake is a system mode method that returns records or fields to the caller. The user then sees data they could not query themselves. Return only what the caller is meant to have, such as a count or a boolean. Alternatively, run the final read in user mode.
Untrusted input in privileged code
System mode makes injection worse. A dynamic query built by string concatenation in system mode can expose any object. Use bind variables with Database.queryWithBinds, and validate object and field names against an allow list.
public with sharing class RollupService {
public static Integer countOpenCases(Id accountId) {
// System mode on purpose: support agents may not see all cases,
// but the count shown on the account must be complete.
return Database.countQueryWithBinds(
'SELECT COUNT() FROM Case WHERE AccountId = :accountId AND IsClosed = false',
new Map<String, Object>{ 'accountId' => accountId },
AccessLevel.SYSTEM_MODE
);
}
}The method takes a typed Id and returns an Integer. The privileged access cannot leak records.
Sharing and mode are not the same switch
User mode enforces the running user's permissions and sharing regardless of the class keyword. System mode does not turn sharing off for you. Treat "mode" and "sharing keyword" as two settings and check both when reviewing code.
Review checklist
- Is each system mode operation in its own small method?
- Does a comment say why the user's own access is not enough?
- Are all inputs typed or validated, with bind variables in dynamic SOQL?
- Does the method return only data the caller may see?
- Is there a test that runs as a low-privilege user and confirms the side effect works while direct access stays blocked?
Exercise
A with sharing controller lets users close a Case. After closing, it must write a row to AuditLog_c, which no user profile can access. Which parts should run in user mode and which in system mode, and what must the logging method avoid doing?
Show answer
Update the Case in user mode (for example, update as user) so the user's own permissions apply. Write the AuditLogc row in a small, separate method using Database.insert(entry, AccessLevel.SYSTEMMODE), with a comment explaining why. The logging method should not return the log record or any other data to the caller, and it should take only validated, typed inputs.