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

This mode is typical for most user-initiated operations, such as Visualforce page interactions and API calls initiated by users.

public class UserModeExample {
    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);
    }

}

This mode is typically used for administrative tasks, batch processing, and Apex code invoked by the system, such as triggers running on records created by automated processes.

public class SystemModeExample {
    @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 execution
Scheduled 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
Custom Metadata Loader execution

User Mode:

Standard Salesforce UI interactions (e.g., creating, editing, deleting records)
Visualforce page interactions
Custom Apex REST Web Service calls from external systems
@future methods execution
Queueable Apex execution
Asynchronous Apex execution
Scheduled Apex execution (when the scheduled job is initiated by a user)
Workflow Rules, Process Builder, and Flows execution (when run in user context)
Email Send actions initiated by users (e.g., Send Email button)
Chatter posts and comments created by users
Community User interactions (e.g., creating, editing records, participating in discussions)
API calls initiated by users (e.g., SOAP API, Bulk API)
External services integration initiated by users

Please note that this is not an exhaustive list, and there may be other operations not mentioned here. Additionally, some operations may be context-dependent and can execute in either system mode or user mode depending on the specific circumstances. It's essential to understand the execution context and mode when designing and developing applications in Salesforce to ensure proper behavior and compliance with security and sharing rules.

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

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach