Skip to main content

Sharing Rules

Enforce User Mode for Database Operations

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, unlike in system mode. User mode always applies sharing rules, but in system mode they’re controlled by sharing keywords on the class

Used methodsModeSharing Mode
none (by default)USER_MODEwith sharing
.systemMode()SYSTEM_MODEinherited sharing
.systemMode().withoutSharing()SYSTEM_MODEwithout sharing
.systemMode().withSharing()SYSTEM_MODEwith sharing

Default

USER_MODE is a enabled by default. It means that the object permissions, field-level security and sharing rules are enforced.

SOSL.find('SearchText')
.inAllFields()
.returning(
SOSL.Returning(Account.SObjectType)
)
.toSearchList();

The object permissions, field-level security, and sharing rules are enforced. Class sharing mode is ignored (without sharing).

public without sharing class ExampleController {
public static List<List<SObject>> searchInAccounts(String searchText) {
return SOSL.find(searchText)
.inAllFields()
.returning(
SOSL.Returning(Account.SObjectType)
)
.toSearchList();
}
}

Customize

Developers can control the sharing mode (inherited sharing, with sharing, and without sharing) only in SYSTEM_MODE. SYSTEM_MODE can be enabled via the .systemMode() method.

Inherited Sharing

NOTE! To make it work, always set inherited sharing in your selector class.

SOSL.find('SearchText')
.inAllFields()
.returning(
SOSL.Returning(Account.SObjectType)
)
.systemMode()
.toSearchList();

The object permissions and field-level permissions are ignored. Sharing rules are controlled by the sharing mode (without sharing).

public without sharing class ExampleController {
public static List<List<SObject>> searchInAccounts(String searchText) {
return SOSL.find(searchText)
.inAllFields()
.returning(
SOSL.Returning(Account.SObjectType)
)
.systemMode()
.toSearchList();
}
}

With Sharing

You can force the sharing mode for all of your queries.

SOSL.find('SearchText')
.inAllFields()
.returning(
SOSL.Returning(Account.SObjectType)
)
.systemMode()
.withSharing()
.toSearchList();

The object permissions and field-level permissions are ignored. Sharing rules are controlled by the sharing mode specified in the query() method (.withSharing()).

public with sharing class ExampleController {
public static List<List<SObject>> searchInAccounts(String searchText) {
return SOSL.find(searchText)
.inAllFields()
.returning(
SOSL.Returning(Account.SObjectType)
)
.systemMode()
.toSearchList();
}
}

Without Sharing

You can force the sharing mode for all of your queries.

SOSL.find('SearchText')
.inAllFields()
.returning(
SOSL.Returning(Account.SObjectType)
)
.systemMode()
.withoutSharing()
.toSearchList();

The object permissions and field-level permissions are ignored. Sharing rules are controlled by the sharing mode specified in the query() method (.withoutSharing()).

public with sharing class ExampleController {
public static List<List<SObject>> searchInAccounts(String searchText) {
return SOSL.find(searchText)
.inAllFields()
.returning(
SOSL.Returning(Account.SObjectType)
)
.systemMode()
.withoutSharing()
.toSearchList();
}
}