Learn Apex
Sharing in Apex
with sharing and without sharing decide whether record-level sharing rules apply to SOQL and DML in that class — independent of CRUD and FLS, which you still enforce separately.
Simulator

In Apex, start from the user story: should this code see the same rows the user sees in the UI? If yes, default to with sharing. If the job truly needs to ignore record visibility (batch, integration, careful admin tooling), isolate without sharing in a small, reviewed class — never sprinkle it everywhere by default.

Mini org: who sees which Account rows?

Three accounts, two reps. Toggle the running user and the class keyword — cards light up when this SOQL would return that row (SELECT Id, Name FROM Account). Teaching model only.

Run as:
Class keyword:
001AA

Acme Corp

Alice is owner

001BB

Beta LLC

Bob is owner

001GG

Gamma Inc

Alice owns; Bob has read via share

Count: 2 rows visible for this combination — compare Alice vs Bob with with sharing, then flip to without sharing and watch all three appear.

Jump to:

Class declaration updates with your keyword

Apex shape
public with sharing class AccountLookup {
    public List<Account> visibleRows() {
        return [SELECT Id, Name FROM Account];
    }
}

Scenario → what teams usually pick

Click a scenario — no single answer fits every org; these are conversation starters with architects. Use the cards to prompt design reviews, not as rigid law.

Lightning / Visualforce page

Pattern: with sharing

Honor the running user’s record access — what they see in the UI should match what Apex returns.

Prefer with sharing when…

  • User-facing queries should mirror list views.
  • You are building controllers or LWC Apex behind UI.
  • Least privilege is the default security story.

Consider without sharing when…

  • A job must process rows the scheduler user cannot see.
  • You deliberately centralize elevated access in one reviewed class.
  • You document why and audit what leaves that class.
Sharing keyword vs. object / field permissions

with sharing controls record visibility (who owns / shares rows). Profile permission sets and permission set assignments still control object and field access — students learn to combine both, and to use WITH SECURITY_ENFORCED or the stripInaccessible pattern when FLS must be enforced in code.