Newer Version Available

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

AccessLevel Class

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

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