---
schema_version: "1"
id: ea1e0e36ae65a0fd198966ceee6b974e233aa4b35e4ba6ee79667b4706943aa5
title: AccessLevel Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_class_System_AccessLevel.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_AccessLevel.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: c0cf60c8dbcf6f4bee8e7d4d7dfd37687c7bd6f08d96a4b15ae0e32fa4bb17e7
description: Defines the different modes, such as system or user mode, that Apex database operations execute in. Supports database and search operations to be run with permissions specified in a permission set. Apex enforces field-level security (FLS) and object permissions as per the specified permission set, in addition to the running user’s permissions.Execution mode in which the the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords.Execution mode in which the object permissions, field-level security, and sharing rules of the current user are enforced.
---
# AccessLevel Class

Defines the different modes, such as system or user mode, that Apex database operations execute in.

## Namespace

[System](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_System.htm "The System namespace provides classes and methods for core Apex functionality.")

## Usage

By default, Apex code runs in user mode, which means that the current user’s object permissions, field-level security, and sharing rules are enforced. You can set a specific DML method to system mode, where the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the [class sharing keywords](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm).

Many of the DML methods of the System.Database and System.Search classes include an accessLevel parameter to specify the execution mode.

## Example

If the user running this Apex code doesn't have write access to the Account object, the Database.insert() method returns an error.

List<Account> toInsert = new List<Account>{new Account(Name = 'Exciting New Account')};

List<Database.SaveResult> sr = Database.insert(toInsert, AccessLevel.USER\_MODE);

In contrast, this example shows the method running in system mode. The success of the insert doesn't depend on whether the user running the Apex code has create access to the Account object.

List<Account> toInsert = new List<Account>{new Account(Name = 'Exciting New Account')};

List<Database.SaveResult> sr = Database.insert(toInsert, AccessLevel.SYSTEM\_MODE);

- **[AccessLevel Methods](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_AccessLevel.htm#apex_System_AccessLevel_methods)**
- **[AccessLevel Properties](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_AccessLevel.htm#apex_System_AccessLevel_properties)**

## AccessLevel Methods

The following are methods for AccessLevel.

- **[withPermissionSetId(permissionSetId)(Developer Preview)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_AccessLevel.htm#apex_System_AccessLevel_withPermissionSetId)**  
    Supports database and search operations to be run with permissions specified in a permission set. Apex enforces field-level security (FLS) and object permissions as per the specified permission set, in addition to the running user’s permissions.

### withPermissionSetId(permissionSetId)(Developer Preview)

Supports database and search operations to be run with permissions specified in a permission set. Apex enforces field-level security (FLS) and object permissions as per the specified permission set, in addition to the running user’s permissions.

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

Feature is available as a developer preview. Feature isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements. All commands, parameters, and other features are subject to change or deprecation at any time, with or without notice. Don’t implement functionality developed with these commands or tools in a production environment. You can provide feedback and suggestions for the “Permission Sets with User Mode” feature in the [Trailblazer Community](https://trailhead.salesforce.com/trailblazer-community/groups/0F94S000000GvrW "HTML (New Window)").

This feature is available in scratch orgs where the ApexUserModeWithPermset feature is enabled. If the feature isn’t enabled, Apex code with this feature can be compiled but not executed.

#### Signature

public System.AccessLevel withPermissionSetId(String permissionSetId)

#### Parameters

- **permissionSetId**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.") Permissions in the specified permission set are enforced while running user-mode DML operations, in addition to the running user’s permissions.

#### Return Value

Type: [Access Level Class](#apex_class_System_AccessLevel "Defines the different modes, such as system or user mode, that Apex database operations execute in.")

#### Example

This example runs the AccessLevel.withPermissionSetId() method with the specified permission set and inserts a custom object.

@IsTest
public with sharing class ElevateUserModeOperations\_Test {
    @IsTest
    static void objectCreatePermViaPermissionSet() {
        Profile p = \[SELECT Id FROM Profile WHERE Name='Minimum Access - Salesforce' WITH USER\_MODE\];
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en\_US',
            LocaleSidKey='en\_US', ProfileId = p.Id,
            TimeZoneSidKey='America/Los\_Angeles',
            UserName='standarduser' + DateTime.now().getTime() + '@testorg.com');

        System.runAs(u) {
            try { 
                Database.insert(new Account(name='foo'), AccessLevel.USER\_MODE); 
                Assert.fail(); 
            } catch (SecurityException ex) { 
                Assert.isTrue(ex.getMessage().contains('Account'));
            }
            //Get ID of previously created permission set named 'AllowCreateToAccount'
            Id permissionSetId = \[SELECT Id FROM PermissionSet 
                WHERE Name = 'AllowCreateToAccount' WITH USER\_MODE LIMIT 1\].Id;

            Database.insert(new Account(name='foo'), AccessLevel.USER\_MODE.withPermissionSetId(permissionSetId)); 

            // The elevated access level in not persisted to subsequent operations
            try { 
                Database.insert(new Account(name='foo2'), AccessLevel.USER\_MODE); 
                Assert.fail(); 
            } catch (SecurityException ex) { 
                Assert.isTrue(ex.getMessage().contains('Account')); 
            } 
            
        } 
    } 
}

## AccessLevel Properties

The following are properties for AccessLevel.

- **[SYSTEM\_MODE](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_AccessLevel.htm#apex_System_AccessLevel_SYSTEM_MODE)**  
    Execution mode in which the the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the [class sharing keywords](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm).
- **[USER\_MODE](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_AccessLevel.htm#apex_System_AccessLevel_USER_MODE)**  
    Execution mode in which the object permissions, field-level security, and sharing rules of the current user are enforced.

### SYSTEM\_MODE

Execution mode in which the the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the [class sharing keywords](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm).

#### Signature

public System.AccessLevel SYSTEM\_MODE {get;}

#### Property Value

Type: System.AccessLevel

### USER\_MODE

Execution mode in which the object permissions, field-level security, and sharing rules of the current user are enforced.

#### Signature

public System.AccessLevel USER\_MODE {get;}

#### Property Value

Type: System.AccessLevel