Secure Apex Code with User Mode Database Operations (Generally Available)
The new Database
and Search
methods support an accessLevel
parameter that lets you run database and search operations in user mode instead of in the default system mode. This feature, now generally available, includes some changes since the last release. Apex code runs in system mode by default, which means that it runs with substantially elevated permissions over the user running the code. To enhance the security context of Apex, you can specify user mode access for database operations. Field-level security (FLS) and object permissions of the running user are respected in user mode and it always applies sharing rules. In system mode, the class sharing keywords control the sharing rules.
Where: This change applies to Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions.
How: You can indicate the mode of the operation by using WITH USER_MODE
or WITH SYSTEM_MODE
in your SOQL query. This example specifies user mode.
Database operations can specify user or system mode. This example inserts a new account in user mode.
The new AccessLevel
class represents the two modes that Apex runs database operations in. Use this new class to define the execution mode as user mode or system mode.
We introduced the beta user mode feature in the Summer ’22 release and added more beta features in the Winter ’23 release. The two beta releases included this initial set of overloaded System.Database
and System.Search
methods that perform DML and query operations.
Database.query
methodDatabase.countQuery
methodDatabase.getQueryLocator
methodsSearch.query
method- Database DML methods (
insert
,insertAsync
,insertImmediate
,update
,updateAsync
,updateImmediate
,upsert
,merge
,delete
,deleteAsync
,deleteImmediate
,undelete
, andconvertLead
)
We added these new methods in this GA release.
Database.queryWithBinds
Database.getQueryLocatorWithBinds
Database.countQueryWithBinds
Comments
Post a Comment