Think in Entry Points
यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।
Module 2 of 7 in the chapter Sharing When Writing Apex: Secure Code, chapter 17 of Securing Salesforce: Sharing and Visibility. Each module stands on its own.
The question this module answers: Every Apex class can say with sharing, without sharing or inherited sharing. Which one goes where, and what happens when one calls another?
The way I teach this is to stop thinking about classes one at a time. Think about your classes as entry points: the top of the call stack, where a user’s action comes into your code. Then think about what they’re going to call, and where you need to set your with and without sharing.
The three keywords
Section titled “The three keywords”Salesforce’s guide, in its own words (Use the with sharing, without sharing, and inherited sharing Keywords (opens in a new tab), Apex Developer Guide, checked 2026-11-26):
- with sharing “to enforce sharing rules of the current user.” When to use it: “Use this mode as the default unless your use case requires otherwise.”
- without sharing “to ensure that the sharing rules for the current user aren’t enforced.” When: “Use this mode with caution. Make sure that you don’t inadvertently expose sensitive data that’s normally hidden by sharing rules. This sharing mode is best used to grant targeted elevation of sharing privileges to the current user.”
- inherited sharing “to enforce the sharing rules of the calling class.” When: “Use this mode for service classes that must be flexible and support use cases with different sharing modes.”
And one sentence that’s easy to miss: “Sharing declarations don’t enforce object-level access or field-level security” (same page). The keyword answers which records. Objects and fields are the access mode’s job, modules 3 and 4.
Entry points: where the decision belongs
Section titled “Entry points: where the decision belongs”An entry point is where a transaction starts: a user clicks, a page loads, a request comes in. Salesforce lists them where it explains inherited sharing. An inherited sharing class “runs in with sharing mode if used as” any of these (same page):
- An Aura component controller
- An
@AuraEnabledmethod called from a Lightning web component - A Visualforce controller
- An Apex REST service
- An asynchronous Apex class
- “Any other entry point to an Apex transaction”
That’s my list of places to look first. Every one of them is a door a user, a site visitor or another system can knock on. Put an explicit keyword on every one, and make it with sharing unless you can write down why not.
The guest is the sharpest case. Chapter 14 already gave you Salesforce’s rule: “If a guest user can execute an @AuraEnabled method in an Apex controller used by a Lightning component, always use the ‘with sharing’ keyword” (chapter 14, module 2).
Shared code: inherited sharing
Section titled “Shared code: inherited sharing”Here’s the case I give the class. You have two entry points. One is with sharing, a page for users. One is without sharing, a nightly job that has to see everything. Both need the same block of code in the middle: the logic that totals an order, say. Which keyword does that shared class get?
Inherited sharing. It takes the mode of whoever called it, so the same code runs with the user’s sharing for the page and without for the job. “Using inherited sharing is an advanced technique to determine the sharing mode at run time and design Apex classes that can run in either with sharing or without sharing mode” (same page).
But read the warning that comes with it: “Because the sharing mode is determined at run time, you must take extreme care to ensure that your Apex code is secure to run in both with sharing and without sharing modes.” And the safety net: “A class declared as inherited sharing runs as without sharing only when explicitly called from an already established without sharing context.” Called directly as an entry point, it’s with sharing.
Does it flip back and forth?
Section titled “Does it flip back and forth?”In class I asked the room a question I hadn’t tested myself: if a with sharing class calls a without sharing class, does the mode truly flip back and forth? Salesforce’s answer: yes, method by method. “the sharing mode of a method is determined by where the method is defined, not where it’s called from,” except in an inherited sharing class. A method in a with sharing class enforces sharing even when a without sharing class calls it (same page). (Salesforce notes exceptions for classes compiled at API 66.0 or earlier.)
That’s what makes the next pattern work. The rest of the rules, from the same page:
- Inner classes keep their own. “Inner classes don’t adopt the sharing mode of the container class.”
- A class with no keyword that extends another “adopts the same sharing mode as the parent class.”
- Async starts over. “Asynchronous Apex classes defined with inherited sharing always run in with sharing mode for asynchronous operations. Each asynchronous operation is a new entry point and the sharing mode isn’t serialized.”
- Anonymous Apex and Connect in Apex “always run in with sharing mode.”
- Triggers can’t have a keyword at all. Module 5 is about them.
Narrow elevation: without sharing, on purpose
Section titled “Narrow elevation: without sharing, on purpose”Salesforce gives a real reason for without sharing: “For example, use without sharing to allow community users to read records to which they wouldn’t otherwise have access” (same page). Done well, it’s narrow: one class, one method, one question, and the user never gets the records themselves.
Take the restaurant ordering site from chapter 15. When a restaurant books a delivery, the site has to check how many deliveries are already booked in that time slot, across every restaurant. The user mustn’t see other restaurants’ orders. They only need the answer: is the slot free?
Here’s that as a sketch (not compiled):
// The entry point: the user's own sharing.public with sharing class DeliveryBookingController { @AuraEnabled public static Boolean isSlotFree(Datetime slot) { return SlotCounter.countBooked(slot) < SlotCounter.CAPACITY; }}
// The narrow elevation: one method, one number back, no records.// Reason: capacity needs every restaurant's bookings; users see none of them.public without sharing class SlotCounter { public static final Integer CAPACITY = 12; public static Integer countBooked(Datetime slot) { return [SELECT COUNT() FROM Order WHERE Delivery_Slot__c = :slot WITH SYSTEM_MODE]; }}Three things make that safe: the without sharing class does one thing, it returns a number and never a record, and the reason is written next to it. Module 7 turns that into a review rule.
The defaults, and why not to rely on them
Section titled “The defaults, and why not to rely on them”“In API version 67.0 and later, classes without an explicit sharing declaration run in with sharing mode.” In 66.0 and earlier, it depends: with sharing if any class in the inheritance chain is at 67.0 or later, or for an Aura controller or an @AuraEnabled method called from a Lightning web component; “Otherwise, the class runs in without sharing mode”; and “If the class isn’t an Apex entry point, its sharing mode is defined by the sharing mode of the calling class” (same page).
Salesforce’s own comment on that list says enough: “Identifying the sharing mode for Apex classes compiled with API version 66.0 or earlier is challenging without an explicit declaration.” It takes an investigation of the inheritance tree, the callers and the class’s behavior (same page). Don’t make anyone investigate. “We recommend that you always include an explicit sharing declaration on Apex classes that include database operations or SOQL queries.”
Test knowledge. Know the three keywords: with sharing enforces the current user’s sharing, without sharing doesn’t, inherited sharing takes the caller’s; and that none of them enforces object or field permissions. Know that inherited sharing runs as with sharing at an entry point (Aura controllers, @AuraEnabled methods from Lightning web components, Visualforce controllers, Apex REST, async Apex) and as without sharing only when called from a without sharing context. Know that a method’s mode comes from the class it’s defined in (except inherited sharing), that inner classes don’t adopt the outer class’s mode, that a class with no keyword adopts its parent’s, that anonymous Apex runs with sharing, that triggers can’t declare one, and that from API 67.0 a class with no keyword runs with sharing. A likely area, not a known question.
What an architect decides here
Section titled “What an architect decides here”- Which classes are entry points, and that every one has an explicit keyword, with sharing unless a written reason says otherwise.
- Where shared service code uses inherited sharing, and that it’s safe in both modes.
- For each without sharing class, the one question it answers, what it returns, and the reason written beside it.
- That guest-facing and site-facing methods are with sharing, always.
- That no class in the org relies on a default, whatever its API version.
Beyond the exam: what AI changes. An Apex action an agent calls is an entry point like any other. Give it an explicit keyword, and if it must be without sharing, make it a narrow elevation that returns an answer, not records. An agent will happily repeat every record a without sharing method hands back. Chapter 20 covers agents.
Explore
Section titled “Explore”- Use the with sharing, without sharing, and inherited sharing Keywords (opens in a new tab), Apex Developer Guide (checked 2026-11-26)
- Apex Classes Enforce Sharing Rules by Default (opens in a new tab), Salesforce Summer ‘26 Release Notes (checked 2026-11-26)
