Why should we use sharing and without sharing in salesforce?

1.9K    Asked by DavidEdmunds in Salesforce , Asked on May 8, 2023

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

Interviews

Parent Categories