23

I am always unsure whether to use with sharing or without sharing in my classes or to just leave that out. The documentation is part of my confusion.

  • Any practical hints?
  • When do you use it and in which types of classes (Domain, Service, Util)?
  • When do you leave it out?

So the perfect answer would be "When I write the header of a new Apex class I put it in when ....".

2
  • I think this help article does a great job of describing the sharing keywords. May 16, 2013 at 14:36
  • Sorry but my confusion started with exactely that article. May 16, 2013 at 21:18

3 Answers 3

25

If not declared explicitly, classes are always without sharing.

Apex without a sharing declaration is insecure by default. Ref

It is important to understand that in both cases (With or Without Sharing), Apex runs in System Mode, so it wouldn't respect things like Field Level Security.

Use With Sharing when the User has access to the records being updated via Role, Sharing Rules, Sales Teams - any sort of sharing really.

Without Sharing is reserved for cases where the User does not have access to the records, but there is a need to update them based on user input.

Also the With / Without Sharing commutes and if a class doesn't have an explicit declaration, the sharing context of the calling class is inherited.

5
  • I guess all the voter already understand the concept ;) Do I understand you right that putting this in a class only makes sense in user triggered classes (Controllers and controller extensions)? May 16, 2013 at 14:20
  • 2
    Yes, also Entity Handler classes, which may be invoked by a trigger, eg AccountAfter delegates processing to AccountHandler, or such. Principally User facing Use case. There may also be a case for it, if you have dedicated API Users which are updating records based on external triggers. I'd say use With Sharing until you really must use Without Sharing. Always safer. May 16, 2013 at 14:41
  • 4
    Referring to your statement - "If not declared explicitly, classes are always With Sharing". In Apex documentation it is explicitly stated that - "If a class isn't declared as either with or without sharing, the current sharing rules remain in effect. This means that if the class is called by a class that has sharing enforced, then sharing is enforced for the called class." So I think you need to correct your answer! Jan 12, 2015 at 12:50
  • The documentation implies that apex it doesn't care about field level security, however, when querying that is indeed the case. The primary one is "No such column 'my_field__c' on entity 'my_object__c'. Nov 9, 2015 at 16:57
  • @RalphCallaway, That is only the case when the field is hidden from all profiles. Normally, if a user does not have visibility to a field the data from that field will still be returned in a query. Jan 26, 2016 at 19:12
5

Go through following link for more detailed explanation..

http://knowsalesforce.blogspot.com/2014/02/salesforce-system-mode-user-mode-and.html?spref=fb

This blog explains how with sharing and without sharing works in context of system and user mode.

Conclusion out of this blog is,

  1. In Salesforce, all apex code run in system mode. It ignores user's permissions. Only exception is anonymous blocks like developer console and standard controllers. Even runAs() method doesn't enforce user permissions or field-level permissions, it only enforces record sharing.

  2. Keywords 'With Sharing & Without Sharing' themselves suggests that they have nothing to do with permissions on object & fields. They only play role in selecting records on sharing basis.

2
  • 2
    Amit, we try to offer information directly within the site so that users do not need to browse away. Please include a summary of the blog that contains a useful answer. Answers only containing an url are often flagged and removed. Feb 18, 2014 at 9:47
  • I hope this will work now.. Feb 18, 2014 at 10:47
2

Use the with sharing keyword when declaring a class to enforce the sharing rules that apply to the current user and if you don't want to enforce it use without sharing keyword.

We can use it in any type of classes build in apex.

If you leave it out i.e. class is not declared as either with or without sharing, the current sharing rules remain in effect. This means that if the class is called by a class that has sharing enforced, then sharing is enforced for the called class.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .