Activity Forums Salesforce® Discussions What is sharing class in Salesforce?

  • Aman

    Member
    July 18, 2017 at 5:19 am

    Hello Raghav,

    Apex generally runs in system context; that is, the current user's permissions, field-level security, and sharing rules aren’t taken into account during code execution.

    Because these rules aren't enforced, developers who use Apex must take care that they don't inadvertently expose sensitive data that would normally be hidden from users by user permissions, field-level security, or organization-wide defaults (OWD in Salesforce). They should be particularly careful with Web services, which can be restricted by permissions, but execute in system context once they are initiated.

    Most of the time, system context provides the correct behavior for system-level operations such as triggers and Web services that need access to all data in an organization. However, you can also specify that particular Apex classes should enforce the sharing rules that apply to the current user

  • shariq

    Member
    September 19, 2018 at 1:44 pm

    Hi,

    With sharing Keyword: This keyword enforces sharing rules that apply to the current user. If absent, code is run under default system context. Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. Use With Sharing when the User has access to the records being updated via Role, Sharing Rules, Sales Teams – any sort of sharing really.

    Example:

    public with sharing class sharingClass {

    // Code here

    }

    Without sharing keyword: Ensures that the sharing rules of the current user are not enforced. 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 may want to explicitly turn off sharing rule enforcement when a class acquires sharing rules when it is called from another class that is declared using with sharing. Without Sharing is reserved for cases where the User does not have access to the records, but there is a need to update them based on user input.

    public without sharing class noSharing {

    // Code here

    }

    Some things to note about 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, if a method is defined in a class declared with with sharing is called by a class declared with without sharing, the method will execute with sharing rules enforced.
    If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. This means that the class doesn’t enforce sharing rules except if it acquires sharing rules from another class. For example, if the class is called by another class that has sharing enforced, then sharing is enforced for the called class.
    Both inner classes and outer classes can be declared as with sharing. The sharing setting applies to all code contained in the class, including initialization code, constructors, and methods.
    Inner classes do not inherit the sharing setting from their container class.
    Classes inherit this setting from a parent class when one class extends or implements another.

    Thanks

  • Parul

    Member
    September 19, 2018 at 1:54 pm

    Adding some point:

    When you use 'with sharing'(with Security Settings enforced) keyword the Apex code runs in User Context, i.e all the sharing rules for the current user are taken into account.

    With Sharing:

    Let's say you're trying to perform an update as the user A.Now If user A cannot edit record R1, then an error will occur. If user A cannot see record R1, they cannot query that record. Use this mode when for pages that should respect sharing settings, or any other time you might want to enforce the rules.

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos