anusha(salesforce developer)

Using WITH SHARING and WITHOUT SHARING keywords on class to Manage Sharing rules in Salesforce

 Use the with sharing or without sharing keywords on a class to specify whether or not to enforce sharing rules.The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class 

1.The sharing setting of the class where the method is defined is applied, not of the class where the method is called.

 Ex: Public with sharing  class ctrl1{
                    //this class enforce to apply sharing rules
                     public void testMethod1{
                     }
          }


        Public without sharing  class ctrl2{
                    //sharing rules are by passed in this class
                    public void testMethod2(){
                                        ctrl1   ct=new ctrl1();
                                           ct.testMethod1();
                                        //this method excutes by enforcing sharing rules.
                    }
         }


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

.
3.Inner classes do not inherit the sharing setting from their container class.

4.Classes inherit this setting from a parent class when one class extends or implements another.

5.If with or without sharing are not mentioned on class then default is " WITHOUT SHARING" 

No comments:

Post a Comment