Activity Forums Salesforce® Discussions What is with sharing and without sharing in salesforce?

  • Prachi

    Member
    December 12, 2019 at 7:00 am

    Hi,

    With Sharing-  Enforce the sharing rules that apply to current user
    The with sharing keyword allows you to specify that the sharing rules for the current user are considered for the class. You have to explicitly set this keyword for the class because Apex code runs in system context. In system context, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied for the current user. This strategy ensures that code doesn’t fail to run because of hidden fields or objects for a user. The only exceptions to this rule are Apex code that is executed with the executeAnonymous call and Chatter in Apex.executeAnonymous always executes using the full permissions of the current user. For more information on executeAnonymous, see Anonymous Blocks.

    Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. For example:

    public with sharing class sharingClass {
    
    // Code here
    
    }

    Without Sharing - Doesn't enforce the sharing rules

    Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are notenforced. For example, you can explicitly turn off sharing rule enforcement when a class is called from another class that is declared using with sharing.

    public without sharing class noSharing {
    
    // Code here
    
    }

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos