Why should we use sharing and without sharing in salesforce?

2.6K    Asked by DavidEdmunds in Salesforce , Asked on May 27, 2024

Why is "without sharing " used in an Apex class explicitly as we know that in the absence of the ' Without sharing' keyword, it is by default in the Apex class?

Answered by Dipesh Bhardwaj

It is required to use with sharing and without sharing in salesforce because it Ensures that the sharing rules of the current user are not enforced. Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example, you may want to explicitly turn off sharing rule enforcement when a class acquires sharing rules when it is called from another class that is declared using sharing. Without Sharing is reserved for cases where the User does not have access to the records, but there is a need to update them based on user input.


public without sharing class noSharing {
// Code here
}

Your Answer

Answers (2)

You can't use Salesforce with or subway surfers without sharing because it nullifies the current user's sharing policies. If you don't want the current user's sharing restrictions implemented when you declare a class, use the sans sharing keywords. 

3 Months

In Salesforce, the terms "with sharing" and "without sharing" are used to control the sharing and visibility of records when Apex classes execute. This distinction is crucial for maintaining security and ensuring that users only access the data they are permitted to see. Here's an overview of why and when to use "with sharing" and "without sharing":


"With Sharing" Keyword
Why Use "With Sharing":

Respect User Permissions and Sharing Rules: By default, Apex code runs in system context, which means it ignores user permissions and field-level security. Using "with sharing" ensures that the Apex code respects the user's sharing rules and permissions.

Security Compliance: It helps in adhering to the principle of least privilege by making sure that users can only access data that they are supposed to.

Prevent Unauthorized Data Access: It prevents users from accessing or manipulating records they do not have access to, which is crucial for maintaining data privacy and security.

  When to Use "With Sharing":

When you need to enforce record-level security.

When dealing with business logic that should adhere to user permissions and sharing rules.

For classes that are exposed to users (e.g., controllers in Visualforce pages or Lightning components).

Example:

public with sharing class MyClass {
    public void myMethod() {
        // Business logic that respects user sharing rules
    }
}"Without Sharing" Keyword
Why Use "Without Sharing":

Elevated Access Needs: Sometimes, business logic requires access to records regardless of the current user's sharing rules. "Without sharing" allows the code to run with system permissions, ignoring the user's sharing settings.

  • Administrative Operations: When performing administrative tasks such as data migrations, integrations, or cleanup operations that require full access to data.
  • Complex Sharing Models: In scenarios where multiple sharing rules might complicate the logic, and full access is necessary for correct processing.Elevated Access Needs: Sometimes, business logic requires access to records regardless of the current user's sharing rules. "Without sharing" allows the code to run with system permissions, ignoring the user's sharing settings.
  • Administrative Operations: When performing administrative tasks such as data migrations, integrations, or cleanup operations that require full access to data.
  • Complex Sharing Models: In scenarios where multiple sharing rules might complicate the logic, and full access is necessary for correct processing.

When to Use "Without Sharing":

  • For administrative or backend operations that need to bypass sharing rules.
  • When performing operations that require access to all records, regardless of the user's permissions.
  • In utility classes that are called by other Apex classes where sharing is not a concern.

public without sharing class AdminUtility {
    public void performDataCleanup() {
        // Business logic that does not respect user sharing rules
    }
}

Key Considerations

Default Behavior: If you do not specify "with sharing" or "without sharing", the class will run in the context of the calling class. If it is the top-level class (e.g., called directly by a trigger), it will run without sharing by default.

Combining Classes: Be mindful when combining classes with different sharing settings. If a class marked "without sharing" calls a method in a "with sharing" class, the "with sharing" rule will be enforced for that method.

Best Practices

  • Explicit Declarations: Always explicitly declare "with sharing" or "without sharing" to avoid ambiguity and ensure that the intended security model is enforced.
  • Audit and Review: Regularly audit and review Apex code to ensure that sharing settings align with security requirements and business logic.
  • Documentation: Document the reasons for using "without sharing" to maintain clarity and assist in future code reviews or audits.

By carefully choosing between "with sharing" and "without sharing," you can strike a balance between necessary access for your Apex logic and adherence to Salesforce's robust security model.


5 Months

Interviews

Parent Categories