Saturday, November 10, 2018

With sharing and Without sharing in salesforce

Today we will try to cover the topic With sharing and Without sharing in Salesforce.

With sharing:

We use the keyword "with sharing" before declaring class so as to take into account the sharing rules for the user.By default apex class runs into system mode so as to avoid code failure because of permissions.

With sharing and Without sharing in salesforce


Syntax:

Public with sharing class someName{       


}

Without sharing:

Sharing rules are not taken into account for the user.

with sharing and without sharing class in salesforce


Syntax:

Public without sharing class someName{


}


POINTS TO NOTE:


  1. If we do not declare class with "With sharing" or "Without sharing"  the class will not take into account the sharing rules but if this class is called from another class which is declared with "With sharing" it will take into account the sharing rules.
  2. IF the class with "With sharing" is calling method of another class with "Without sharing" than the method inside "Without sharing" class will execute without sharing rules.
  3. IF the class with "Without sharing" is calling method of another class with "With sharing" than the method inside "With sharing" class will execute with sharing rules.
  4. Inner class do not have sharing settings of parent class.
  5. Class have sharing settings of parent class when we implements it or extends it from parent class.

QUESTIONS:


1)Let say user do not have permission on a child object and he is having permission on parent object to read/create/edit/delete parent object,If I create a trigger on parent object to insert a child record after parent record is created,will it create a child record or not after user insert parent record manually?

It will create a child record. (Trigger/apex class runs in system mode)

2)If in the above case from trigger I am calling apex class which is in "with sharing" mode and where i am inserting child record after parent is inserted manually by user so will it create a child record?

It will create a child record.(With sharing keyword has nothing to do with user permissions).


**************************************************************************************************************

TAGS:with sharing and without sharing in salesforce,with sharing and without sharing class in salesforce,difference between with sharing and without sharing class in salesforce,with sharing in salesforce,without sharing in salesforce.

**************************************************************************************************************

No comments:

Post a Comment