What is the purpose of "without sharing" keyword?

Even though a class without any sharing keyword by default is without sharing, it is mandatory to mention without sharing if the class will be called by another class.

If a with sharing class calls a class without any sharing keywords, then sharing rules are enforced.

If a with sharing class calls a class with without sharing keyword, then sharing rules are not enforced.

Cheers!!!

2 comments:

  1. public class Three {

    public void section(){
    for(integer i=0;i<=3;i++){
    Contact con=new Contact(lastname='Kumar'+i, Email='kkkkk.0455@email.com');
    insert con;
    }
    }
    }
    ------------------------------------------------
    public class One {

    Three th= new Three();
    List mm=[select id, Counting__c,Edit_Counting__c from MapTesting__c where id='a0C2800000BRk1Q'
    or id='a0C2800000BRk6F' or id='a0C2800000BRk1a'];
    public void main(){
    for(MapTesting__c maap : mm){

    th.section();
    maap.Counting__c= maap.Edit_Counting__c+1;
    update maap;
    System.debug('maap:'+maap);
    }
    }
    }
    --------------------------------------------
    Hi Murugan,
    In class One i called class Three.
    In the above two classes i did not mention with/without sharing keyword and it is not giving any syntax error and Execution Error. But you mentioned in this document "it is mandatory to mention without sharing if the class will be called by another class".

    Instead of does not giving any error, i did not understand why you mentioned "it is mandatory to mention without sharing if the class will be called by another class".

    Please give a brief explanation with an example.
    Thanks in advance
    KS Kumaar

    ReplyDelete
    Replies
    1. It is mandatory if it is going to be called by another class. If we don't mention, the calling class's sharing is enforced.

      http://www.infallibletechie.com/2012/12/class-with-sharing-and-without-sharing.html

      Delete