Understanding Sharing Context in Apex Development
Raptbot Technologies Private Limited
Elevate Your Business with Raptbot: Your Premier Salesforce Partner for Innovation, Integration, and Unmatched Expertise
In Salesforce, who can see what is controlled by something called "sharing." But sometimes, when we make Apex classes, we forget about this sharing stuff. It's important to remember that Salesforce says, "Apex without an explicit sharing declaration is insecure by default." So, we need to always think about sharing when we write Apex code.
There are three simple sharing keywords we can use in Apex:
If we don't use any of these sharing keywords in our Apex class, Salesforce won't enforce any sharing rules unless the class that calls ours has sharing rules set up.
Now, let's talk about what happens when we use inherited sharing versus when we don't say anything about sharing:
If we use inherited sharing in our class and it's the starting point of a process in Apex, Salesforce automatically applies sharing rules. But if we don't mention sharing at all, Salesforce won't automatically apply sharing rules, even if other classes in the process have sharing rules.
Understanding these simple sharing rules in Salesforce Apex helps us keep data safe and follow Salesforce's best practices. It's all about making sure the right people see the right things at the right time!