Access Level Class in Salesforce!
Do you know about the AccessLevel class in Salesforce?
It plays a crucial role in determining the execution mode of Apex database operations.
Let's explore this class to gain deeper insights into its usage and benefits.
By default, Apex code runs in system mode, which gives it substantially elevated permissions. In this 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.
However, in user mode, the current user's object permissions, field-level security, and sharing rules are enforced.
To specify the execution mode, many DML methods of the System.Database and System.Search classes include an accessLevel parameter.
For example, consider the following code snippet:
List<Account> toInsert = new List<Account>{new Account(Name = 'Exciting New Account')};
List<Database.SaveResult> sr = Database.insert(toInsert, AccessLevel.USER_MODE);
In this case, if the user running the code doesn't have write access to the Account object, the Database.insert() method will return an error.
On the other hand, running the same code with `AccessLevel.SYSTEM_MODE` allows the insert to be successful, irrespective of the user's create access to the Account object.
AccessLevel Properties:
- SYSTEM_MODE:
Execution 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.
- USER_MODE:
Execution mode where the object permissions, field-level security, and sharing rules of the current user are enforced.
Follow Us