Share Your Experience With Others

With Sharing, Without Sharing and Inherited Sharing

Use the with sharing or without sharing keywords on a class to specify whether sharing rules must be enforced. Use the inherited sharing keyword on an Apex class to run the class in the sharing mode of the class that called it.

With Sharing:

with sharing

Without Sharing

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 can explicitly turn off sharing rule enforcement when a class is called from another class that is declared using with sharing. For example:

without to with sharing method call.png

Points while using with or without sharing keywords

  • The sharing setting of the class where the method is defined is applied, not of the class where the method is called. For example:with to without sharing.pngwithout to with sharing method call.png
  • If a class isn’t declared as either with or without sharing, the class runs as without sharing and the class doesn’t enforce sharing rules except when it acquires sharing rules from another class. For example,without any keyword to with sharing
  • Both inner classes and outer classes can be declared as with sharing. For example:innerclasses.png

Inherited Sharing

Using inherited sharing enables you to pass AppExchange Security Review and ensure that your privileged Apex code is not used in unexpected or insecure ways.

inherited sharing.png

 

Guys i have written this blog after reading a lot if i have missed something or something mentioned is wrong then please tell me in comments.

Source : Apex Developer Guide

 

5 responses to “With Sharing, Without Sharing and Inherited Sharing”

  1. sanjay Avatar
    sanjay

    best one and original words, not copied from any other blog

    Like

    1. Kamaal Ahmad Usmani Avatar
      Kamaal Ahmad Usmani

      Thnx Sanjay

      Like

  2. Sudhir Avatar
    Sudhir

    Explained it very well

    Like

  3. Pooja Avatar
    Pooja

    Very well explained.

    Like

  4. Salesforce Permissions and Access: Comprehensive Learning Plan - DreamInForce.com Avatar
    Salesforce Permissions and Access: Comprehensive Learning Plan – DreamInForce.com

    […] with sharing: Enforces that the code respects sharing rules. That means all SOQL queries in that class will only return records the running user is allowed to see (unless the code uses an inner class without sharing or explicitly does something else). Use this for classes that should only operate on data the user normally has access to. For example, a Lightning Aura or LWC controller that fetches case records for the logged-in support agent should be with sharing so that the agent only gets cases they’re permitted to see.  (With Sharing, Without Sharing and Inherited Sharing – SFDCWallah) […]

    Like

Leave a reply to Pooja Cancel reply