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.

List<Account> acc = [SELECT Id FROM Account WITH USER_MODE];

Database operations can specify user or system mode. This example inserts a new account in user mode.

Account acc = new Account(Name='test');
insert as user acc;

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 method
  • Database.countQuery method
  • Database.getQueryLocator methods
  • Search.query method
  • Database DML methods (insertinsertAsyncinsertImmediateupdateupdateAsyncupdateImmediateupsertmergedeletedeleteAsyncdeleteImmediateundelete, and convertLead)

We added these new methods in this GA release.

  • Database.queryWithBinds
  • Database.getQueryLocatorWithBinds
  • Database.countQueryWithBinds

Comments

Popular posts from this blog

IsVisibleInSelfService on Task salesforce

Too many batch retries in the presence of Apex triggers and partial failures.

Governor limit SOQL 101 being suppressed