User and System Mode in Salesforce
User Mode:
When an Apex class executes in user mode, it runs with the permissions and privileges of the current user who initiated the operation, also known as the executing user.
In user mode, Apex code adheres to the organization's sharing rules, field-level security (FLS), and other user-specific permissions and settings.
The visibility and access to records and data are determined by the security settings and permissions assigned to the executing user, including their role, profile, permission sets, and sharing settings.
User mode is typical for most Apex transactions, such as trigger execution, Visualforce controller methods, and custom Apex code invoked by users through the Salesforce UI or API.
System Mode:
When an Apex class executes in system mode, it runs with elevated permissions and bypasses the organization's sharing rules, FLS, and other user-specific permissions.
In system mode, Apex code can perform operations that are not available to the executing user, such as querying or updating records that the user does not have access to under normal circumstances.
System mode is commonly used for administrative tasks, batch processing, scheduled jobs, and Apex code that is invoked by the system, such as triggers running on records created by automated processes.
To execute code in system mode, it must be explicitly invoked by system processes or scheduled jobs, or it must be annotated with the @Future, @Queueable, or @InvocableMethod annotations.
Examples
public static void getUserDetails() {
// Query user details in user mode
User currentUser = [SELECT Id, Name, Email FROM User WHERE Id = :UserInfo.getUserId()];
System.debug('Current User Details: ' + currentUser);
}
}
@future
public static void updateRecordsInSystemMode(List<Id> recordIds) {
// Update records in system mode
List<Carrier_Service__c> recordsToUpdate = [SELECT Id, Name FROM Carrier_Service__c WHERE Id IN :recordIds];
for (Carrier_Service__c record : recordsToUpdate) {
// Perform updates without considering user permissions
record.Fuel_Surcharge__c = true;
}
update recordsToUpdate;
}
}
Here's a list of common operations and their respective execution modes:
System Mode:
Apex Batch Apex executionScheduled Apex execution
Apex trigger execution (except for @future methods)
Apex REST Web Service execution
Lightning Process Builder execution
Workflow Rules execution
Validation Rules execution
Assignment Rules execution
Escalation Rules execution
Email Services execution
Apex REST Web Service execution
Global actions execution (e.g., Quick Actions, Global Quick Actions)
Flow execution (when run in system context)
Platform Event trigger execution
To design and implement secure and compliant Apex code, one must consider the implications of user mode and system mode. It is recommended to execute Apex code in user mode, respecting security controls, and use system mode only when elevated privileges are essential. Error handling, validation checks, and safeguarding Salesforce data are crucial for preventing security vulnerabilities.
Comments
Post a Comment