APEX ACCESS MODIFIERS - With Sharing • Without Sharing

Salesforce Apex Access Modifiers

Access Modifiers

Private: 

  • If you declare a class as a private, it is only known to the block in which it is declared.
  • By default all the inner classes are private.

Public:

  • If you declare a class as a public, this apex class is visible throughout your application and you can access the application anywhere.

Global:

  • If you declare a class as a global, this apex class is visible to all the apex application in the application or outside the application.
  • Note: If a method or a class (inner) is declared as global then the top level class also must be declared as global.

With Sharing:

  • If you declare a class as a With Sharing, sharing rules given to the current user will be taken into consideration and the user can access and perform the operations based on the permissions given to him on objects and fields. (field level security, sharing rules)

Without Sharing:

  • If you declare a class as a Without Sharing then this apex class runs in system mode which means apex code has access to all the objects and fields irrespective of current users sharing rules, fields level security, object permissions.
  • Note: If the class is not declared as With Sharing or Without Sharing then the class is by default taken as Without Sharing.
  • Both inner class and outer classes can be declared as With Sharing.
  • If inner class is declared as With Sharing and top-level class is declared as without sharing, then the entire context will run in With Sharing context.
  • Outer class is declared as With Sharing and inner class is declared as Without Sharing then inner class runs in Without Sharing context only. (Inner classes don't take the sharing properties from outer class).

Virtual:

  • If a class is declared with keyword Virtual then this class can be extended (Inherited) or this class method can be overridden by using a class called overridden.

Abstract:

  • This class contains Abstract methods, which will provide common method implementation to all subclasses.
Now let's see an example and understand how to use it in apex class.

Example 1:
=============================================
public class outerclass
 {
   //code
   
    class innerclass
   {
      //Innerclass code
   }
}
=============================================

Example 2:
=============================================
public with sharing class sharing class
{
    //code
}
    =============================================

    Example 3:
    =============================================
    public without sharing class noSharing
    {
       //code  
    }
    =============================================

    Example 4:
    =============================================
    public with sharing class outer
    {
      //outer class code

      without sharing class inner
      {
        //Inner class code
      }
    }
    =============================================

    In the above code outer class runs with current users sharing rules. But inner class runs with system context.

    Example 5:
    =============================================
    public without sharing class outer
    {
      //Outer class code
      with sharing class inner
      {
        //Inner class code 
      }
    }
    =============================================

    In this way, both the inner and outer classes run with current users permissions.

    WOHOOO !! YOU HAVE JUST COMPLETED APEX ACCESS MODIFIER EPISODE
    If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
    Happy learning ☁️⚡️ (Learn. Help. Share.)

    << PREVIOUS                                                    NEXT >>

    APEX ACCESS MODIFIERS - With Sharing • Without Sharing APEX ACCESS MODIFIERS - With Sharing • Without Sharing Reviewed by on Rating: 5

    No comments:

    HELP !! SHARE !! SUGGEST !!

    Powered by Blogger.