Activity Forums Salesforce® Discussions What is the use of 'with sharing' and 'without sharing' keywords in Salesforce?

  • Nikita

    Member
    September 26, 2019 at 11:45 am

    Hi,

    with sharing: 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 a system context. In the 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.

    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: 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.

    public without sharing class noSharing {
    
    // Code here
    
    }

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos