About this question
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?
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?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Salesforce Expert
Answered on May 27, 2024
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
}
Best Answer · By JanBask Salesforce Expert
Ranjana Admin JanBask Expert
Answered on May 27, 2024
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.
When to Use "Without Sharing":
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
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.
Larry Mcleod Latest answer
Answered on Aug 5, 2024
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.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Salesforce guides from industry experts
Common Salesforce interview questions, answered
Guides, tips and career advice on Salesforce from JanBask experts.
Salesforce How to Become a Salesforce Developer Without Experience
No coding background? Learn how to become a Salesforce Developer step by step - skills to build, projects to create, and how to…
Salesforce How to Become a Salesforce Admin With No Experience
No Salesforce Admin experience? Learn how to build real skills, create projects, and land an entry-level Salesforce Admin job…
Salesforce How Long Does It Take to Become a Salesforce Developer?
Wondering how long to learn Salesforce Developer? Most beginners need 6–12 months. See the study time, stage-by-stage roadmap and…
Salesforce How Long Does It Take to Become a Salesforce Admin?
Wondering how long to learn Salesforce Admin? Most beginners need 3–6 months. See the study time, stage-by-stage roadmap and…