Newer Version Available

This content describes an older version of this product. View Latest

AccessLevel Class (Beta)

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

This feature is a Beta Service. Customer may opt to try such Beta Service in its sole discretion. Any use of the Beta Service is subject to the applicable Beta Services Terms provided at Agreements and Terms.

Salesforce doesn’t guarantee general availability of this feature within any particular time frame or at all, and we can discontinue it at any time. It’s offered as is and isn’t supported, and Salesforce has no liability for any harm or damage arising out of or in connection with it. You can provide feedback and suggestions for the feature in the Trailblazer Community.

Note

Namespace

System

Usage

By default, Apex code runs in system mode, which means that it runs with substantially elevated permissions over the user running the code. In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the current user's object permissions, field-level security, and sharing rules are enforced.

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.

1List<Account> toInsert = new List<Account>{new Account(Name = 'Exciting New Account')};
2
3List<Database.SaveResult> sr = Database.insert(toInsert, AccessLevel.USER_MODE);
4

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.

1List<Account> toInsert = new List<Account>{new Account(Name = 'Exciting New Account')};
2
3List<Database.SaveResult> sr = Database.insert(toInsert, AccessLevel.SYSTEM_MODE);
4

AccessLevel Properties

The following are properties for 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.

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