Apex Sharing and Security: Enforcing Data Access Controls

Apex Sharing and Security: Enforcing Data Access Controls

Table of Contents

  1. Introduction
  2. Understanding Apex Sharing and Security
  3. Enforcing Sharing Rules
  4. Enforcing Object and Field Permissions
  5. Enforce User Mode for Database Operations
  7. Class Security
  8. Security Tips for Apex and LWC
  9. Conclusion


In today’s digital age, data security is of paramount importance for businesses across industries. With sensitive information residing in Salesforce orgs, it becomes crucial to enforce robust data access controls to safeguard against unauthorized access or data breaches. Apex Sharing and Security provide powerful tools to enforce data access controls within the Salesforce ecosystem. In this blog post, we will delve into the various aspects of Apex Sharing and Security, exploring the enforcement of sharing rules, object and field permissions, user mode for database operations, filtering SOQL queries using WITH SECURITY_ENFORCED, class security, and essential security tips for Apex and LWC development.

Understanding Apex Sharing and Security

Before diving deeper into the implementation details, let’s first understand what Apex Sharing and Security entail. Apex Sharing refers to the mechanism of determining the visibility and accessibility of records for users within an organization. Salesforce provides multiple sharing models, such as Organization-Wide Defaults, Role Hierarchy, Sharing Rules, and Manual Sharing, to govern record access.

On the other hand, Apex Security focuses on enforcing object and field permissions, ensuring that only authorized users can perform specific operations on records. Apex Security also includes techniques to enhance the overall security posture of your Apex and Lightning Web Component (LWC) codebase. By combining Apex Sharing and Security, organizations can establish granular data access controls, ensuring data privacy, compliance, and maintaining the integrity of their Salesforce data.

Enforcing Sharing Rules

Sharing Rules play a vital role in determining record visibility beyond the organization-wide defaults. They enable targeted sharing of records based on specified criteria. With Apex, you can programmatically enforce sharing rules, thereby providing fine-grained access to records.

In Salesforce, sharing rules are used to extend access to records, and they are typically defined in the Salesforce user interface. However, there might be scenarios where you need to enforce sharing rules programmatically in Apex, especially when performing operations on records where the sharing rules might not be automatically applied.

Here’s a simple example of how you can enforce sharing rules in Apex using the with sharing keyword in a class or a specific method. This keyword enforces the sharing rules defined in the Salesforce user interface for the current user:

public with sharing class MySharingEnforcedClass {
    public void doSomethingWithRecords() {
        // Your logic here
        List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 10];
        // Perform some operations on the records
        for (Account acc : accounts) {
            // Your logic here
            // For example, update the record
            acc.Name = 'Updated Name';
        // Update records with sharing rules enforced
        update accounts;

In the above example, the with sharing keyword is used in the class declaration. This ensures that the sharing rules are enforced when performing any DML (Data Manipulation Language) operations, such as updating records.

It’s important to note that using with sharing is not necessary in all scenarios, and you should carefully consider whether to use it based on your specific requirements. In some cases, you might need to use without sharing if you want to bypass sharing rules.

Additionally, you can use the Database class methods, such as Database.update(), with the allOrNone parameter set to false to allow partial updates when enforcing sharing rules. This can be useful in scenarios where you want to update as many records as possible, even if some of them fail due to sharing rule violations.

Database.SaveResult[] updateResults = Database.update(accounts, false);

Enforcing Object and Field Permissions

Apart from record-level security, it is equally important to enforce object and field permissions to operations performed on specific objects and their fields. Object and field permissions dictate whether user can view, edit, delete, or create records of a particular object.

In Apex, you can use the Schema class to dynamically check and enforce object and field-level permissions. By utilizing methods like isAccessible(), isCreateable(), isUpdateable(), and isDeletable(), you can validate user permissions before allowing them to perform specific actions on objects and fields. This way, you can prevent unauthorized access or modification of sensitive data within your Salesforce org.

Here’s an example illustrating the usage of isAccessible(), isCreateable(), isUpdateable(), and isDeletable()

public class FieldPermissionsExample {

    public static void checkFieldPermissions(String objectApiName, String fieldName) {
        // Get the SObjectDescribeResult for the object
        Schema.SObjectType objectType = Schema.getGlobalDescribe().get(objectApiName);
        Schema.DescribeSObjectResult objectDescribe = objectType.getDescribe();

        // Check if the field is accessible
        if (objectDescribe.fields.getMap().containsKey(fieldName)) {
            Schema.DescribeFieldResult fieldDescribe = objectDescribe.fields.getMap().get(fieldName);

            // Check if the field is accessible
            if (fieldDescribe.isAccessible()) {
                System.debug(fieldName + ' is accessible.');
            } else {
                System.debug(fieldName + ' is not accessible.');

            // Check if the field is createable
            if (fieldDescribe.isCreateable()) {
                System.debug(fieldName + ' is createable.');
            } else {
                System.debug(fieldName + ' is not createable.');

            // Check if the field is updateable
            if (fieldDescribe.isUpdateable()) {
                System.debug(fieldName + ' is updateable.');
            } else {
                System.debug(fieldName + ' is not updateable.');

            // Check if the field is deletable
            if (fieldDescribe.isDeletable()) {
                System.debug(fieldName + ' is deletable.');
            } else {
                System.debug(fieldName + ' is not deletable.');
        } else {
            System.debug('Field ' + fieldName + ' does not exist on object ' + objectApiName + '.');

    public static void main(String[] args) {
        // Example usage
        checkFieldPermissions('Account', 'Name');
        checkFieldPermissions('Contact', 'NonExistentField');

In this example:

  • isAccessible() checks whether the current user has read access to the field.
  • isCreateable() checks whether the current user has permission to create a new record and set a value for the field.
  • isUpdateable() checks whether the current user has permission to update an existing record and modify the value of the field.
  • isDeletable() checks whether the current user has permission to delete a record and its associated field value.

This code will help you understand the field permissions for a specified object and field. Adjust the objectApiName and fieldName parameters in the checkFieldPermissions method based on your requirements.

Enforce User Mode for Database Operations

Sometimes, it becomes necessary to enforce specific permissions during database operations, regardless of the current user’s actual permissions. Apex provides techniques to temporarily switch to a higher privileged user mode, known as “without sharing.” This allows you to bypass sharing rules and object permissions for specific scenarios that demand elevated access.

By utilizing the System.runAs(User) method, you can execute code as a specific user, overriding their actual permissions. However, it is important to exercise caution while implementing user mode enforcement, as it essentially bypasses the established data access controls. Proper governance and auditing should be in place to ensure abuse of this privilege is prevented, and compliance requirements are met.

Here’s an example of enforcing user mode for database operations:

public class EnforceUserModeExample {

    public static void performDatabaseOperations() {
        // Perform some database operations
        Account acc = new Account(Name = 'Test Account');
        insert acc;

        // Query the inserted record
        Account queriedAccount = [SELECT Id, Name FROM Account WHERE Id = :acc.Id LIMIT 1];
        System.debug('Queried Account: ' + queriedAccount);

        // Update the record
        queriedAccount.Name = 'Updated Test Account';
        update queriedAccount;

        // Delete the record
        delete queriedAccount;

    public static void main(String[] args) {
        // Specify the user for whom you want to enforce user mode
        User targetUser = [SELECT Id FROM User WHERE Username = 'targetuser@example.com' LIMIT 1];

        // Enforce user mode for database operations
        System.runAs(targetUser) {

In this example:

  1. The performDatabaseOperations method contains the database operations you want to perform.
  2. The main method specifies the target user (targetUser) for whom you want to enforce user mode. You should replace 'targetuser@example.com' with the actual username of the target user.
  3. The System.runAs(targetUser) block is used to execute the performDatabaseOperations method in the context of the specified user. This ensures that the database operations are performed with the permissions of the target user.

Note: Enforcing user mode using System.runAs(User) is primarily used for testing scenarios or ensuring that certain operations are performed with specific user permissions. It’s important to handle user mode enforcement carefully and only use it when necessary.


When querying data using SOQL (Salesforce Object Query Language), it is crucial to filter the results based on the current user’s permissions to avoid data leakage. Salesforce provides the WITH SECURITY_ENFORCED clause to automatically apply object and field permissions in SOQL queries.

By simply appending WITH SECURITY_ENFORCED to your SOQL queries, Salesforce dynamically filters the records based on the executing user’s permissions. This prevents the retrieval of data that the user is not authorized to access. Adopting this approach ensures that your code adheres to the defined data access controls, fortifying your application against unauthorized data exposure.

Here’s an example:

public class SecurityEnforcedExample {

    public static void queryWithSecurityEnforced() {
        // Query all accounts using WITH SECURITY_ENFORCED
        List<Account> securedAccounts = [SELECT Id, Name FROM Account WITH SECURITY_ENFORCED];

        // Iterate over the results
        for (Account acc : securedAccounts) {
            System.debug('Queried Account: ' + acc);

    public static void main(String[] args) {
        // Call the method to query accounts with security enforced

In this example:

  1. The queryWithSecurityEnforced method uses the WITH SECURITY_ENFORCED clause in the SOQL query to retrieve only the records that the current user has permission to access.
  2. The main method calls queryWithSecurityEnforced to execute the SOQL query with record-level security enforcement.

By using WITH SECURITY_ENFORCED, Salesforce ensures that the records returned by the query adhere to the current user’s object and field-level permissions, providing an additional layer of security.

Keep in mind that this clause is automatically applied when querying records in Lightning components, but using it explicitly in your Apex code can be beneficial in scenarios where you need to ensure security enforcement.

Class Security

In addition to enforcing sharing rules, object and field permissions, and querying data securely, it is essential to implement secure coding practices within your Apex classes. Secure coding helps mitigate vulnerabilities and safeguard against potential security threats.

Here are some class security best practices to consider:

  • Avoid using the “without sharing” keyword unless absolutely necessary, as it bypasses data access controls.
  • Always sanitize and validate user inputs to prevent potential injection attacks.
  • Utilize appropriate exception handling mechanisms to enhance error handling and prevent sensitive information leakage.
  • Regularly review and update code to align with the latest security best practices and patches.
  • Leverage custom settings or custom metadata to store sensitive configuration data securely.
  • Collaborate with your organization’s security team to ensure compliance with regulatory and legal requirements.

Security Tips for Apex and LWC

To wrap up our exploration of Apex Sharing and Security, let’s discuss some essential security tips for Apex and LWC development:

  1. Adopt the Principle of Least Privilege: Assign users the minimum permissions required to perform their tasks effectively. This helps minimize the potential impact of compromised user accounts.
  2. Implement Two-Factor Authentication (2FA): Enable 2FA for all Salesforce users to add an extra layer of security during login.
  3. Regularly Monitor and Review User Access: Periodically review user access privileges, disabling or removing unnecessary permissions or inactive users.
  4. Stay Updated with Platform Security Features: Keep track of Salesforce’s security releases and enhancements to leverage new features and stay ahead of emerging threats.
  5. Conduct Security Testing and Code Reviews: Regularly run security scans, vulnerability assessments, and code reviews to identify and patch potential security weaknesses.
  6. Educate Users on Security Best Practices: Promote user awareness of common security threats, social engineering techniques, and encourage the use of secure practices like creating strong passwords and safeguarding login credentials.

By following these security tips and incorporating Apex Sharing and Security into your development practices, you can ensure a robust and reliable data access control mechanism, bolstering the overall security posture of your Salesforce org.


In this blog post, we delved into the world of Apex Sharing and Security, understanding their significance in enforcing data access controls within the Salesforce ecosystem. We explored various aspects, including enforcing sharing rules, object and field permissions, user mode for database operations, filtering SOQL queries using WITH SECURITY_ENFORCED, class security best practices, and essential security tips for Apex and LWC development.

By seamlessly integrating primary keywords like “Apex Sharing and Security: Enforcing Data Access Controls” and related keywords such as “Enforcing Sharing Rules,” “Enforcing Object and Field Permissions,” and “Security Tips for Apex and LWC,” we have crafted an informative and engaging blog post. Remember, data security is a shared responsibility, and by implementing these strategies and best practices, you can fortify your Salesforce org against potential threats and ensure data privacy and compliance.

Take the first step towards a more secure Salesforce environment today! Start by reviewing and enhancing your organization’s data access controls, understanding the principles of Apex Sharing and Security, and embracing secure coding practices. Together, we can create a safer digital landscape for businesses to thrive in.

Reference Links:

Apex Release Notes

Leave a Reply

Your email address will not be published. Required fields are marked *