Difference between with Sharing and without sharing keywords in Salesforce
with sharing and without sharing in salesforce

Difference between with Sharing and without sharing keywords in Salesforce

In this blog post, we will learn about the with Sharing and without sharing keywords.We use with sharing keyword on a class to enforce sharing rules. When you use With Sharing keyword on a class, it will take into account the sharing rules of the current user. If we don’t use this keyword on a class, in that case apex code will run in system context.
In system context, apex will have access to all objects and its fields-any kind of object permissions, field-level security or sharing rules will not be applied for the current user.

Running the code in system context ensures that the code will not fail to run because of hidden fields or object restrictions. The only exception is in the case of execute anonymous window and chatter in apex. Execute anonymous will always run using full permissions of the current user.

How to use with Sharing keyword on a Class?

 

[sourcecode lang=”Java”]
public with sharing class HelloWorld
{

}

[/sourcecode]

How to specify without sharing on a class?

When you use Without Sharing on a class, the sharing rules of the current user will not be enforced.

[sourcecode lang=”Java”]
public without sharing class SayHi
{

}
[/sourcecode]

More about with Sharing and Without Sharing Keywords:

  • The sharing settings of the class in which the method is defined is applied,not of the class where the method is called.
    So if there is a method which is defined in a Class which has with sharing enabled but the method is called in a class which doesn’t have sharing(without sharing) enabled,the method will execute with sharing rules enforced.
  • We can declare inner and outer classes with sharing keyword. The sharing setting will be applied to all the code present in the class including intialization code, methods and constructors.
  • Inner classes do not inherit the sharing setting from the outer class.
    Classes inherit sharing setting from their parent class when one class extends from another class.

 

Let’s take a look at the below example:

In this example, we have declared a class with inherited sharing keyword. We have also created a visualforce page in order to call the apex code. Since we have used inherited sharing keyword with the class, only contacts for which the running user has access to will be displayed. If we remove the inherited sharing keyword in the class, in that case it will display all the contact records including the records which current user has no access to.

[sourcecode lang=”Java”]
public inherited sharing class ClassWithSharing
{
public List getAllTheUsers()
{
return [select name,id from contacts];
}
}

[/sourcecode]

Visualforce Page:

[sourcecode lang=”Html”]
<apex:page controller=’ClassWithSharing’>
<apex:repeat value='{!AllTheUsers}’ var=’record’>
{!record.Name}
</apex:page>
[/sourcecode]

To learn more: with sharing and without sharing 

This Post Has 8 Comments

  1. Wasim

    Thanks for the explanation. But I couldn’t understand what’s the difference between ‘with sharing’ and ‘inherited sharing’.

  2. priense

    Cephalexin Mixed With Diclofenac Effects adorroft [url=https://xbuycheapcialiss.com/]cialis online[/url] Heepsy nicotinic acid with cialis side effect

  3. Bruce Wiebusch

    Thanks for revealing your ideas. The first thing is that students have a solution between federal government student loan and a private student loan where it’s easier to go with student loan debt consolidation than through the federal education loan.

  4. Lena Brazzle

    What a lovely blog page. I will definitely be back again. Please keep writing!

  5. Marcell Jarrar

    Wonderful paintings! That is the kind of info that should be shared across the net. Shame on the seek for not positioning this put up higher! Come on over and seek advice from my site . Thanks =)

  6. Gil Bolls

    Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it

  7. Malvina Junghans

    I want going to comment as this posts a bit old now, but just wanted to say thanks.

  8. Lynette Neuberger

    Intimately, the post is actually the freshest on that laudable topic. I fit in with your conclusions and will certainly thirstily look forward to your approaching updates. Simply saying thanks can not simply be acceptable, for the excellent lucidity in your writing. I definitely will immediately grab your rss feed to stay informed of any updates. Solid work and also much success in your business enterprize!

Leave a Reply