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.