I had this use case where i need to run the entire class in 'respecting sharing rules of org'

But in there i had to query Entitysubscirption object

list<EntitySubscription>listt = [Select c.SubscriberId ,c.parentid From EntitySubscription  c where c.parentid in :Allremaningids and c.subscriberId = :UserInfo.getUserId()  ];

This will give me below error when run as a non-admin user.

Implementation restriction: EntitySubscription only allows security evaluation for non-admin users when LIMIT is specified and at most 1000 

Now how i dealt with this,

  1. I marked the whole class as without sharing. This works because as its obvious,it runs as system admin.But disadvantage was i had to write explicit queries to enforce sharing and maintenance in future is going to be difficult ,if sharing changes.
  2. I marked all the class as with sharing. I declared an inner class without sharing,executed the query there . Then i create object in outerclass,call list listt = obj.method(Id id); This works as i think innerclass is being run in without sharing and other class respects sharing.

Now i dont really know whether there is any other disadvantage for this approach?like limits etc?Anyone any idea?

  • I believe apex classes are run in the sharing mode of the calling class, are you sure you've tested this correctly ? Feb 3, 2014 at 6:59
  • salesforce.com/us/developer/docs/apexcode/Content/… 'The sharing setting of the class where the method is defined is applied, not of the class where the method is called. For example, if a method is defined in a class declared with with sharing is called by a class declared with without sharing, the method will execute with sharing rules enforced.'
    – sfdc99999
    Feb 3, 2014 at 7:08
  • I thought above suggests--its the class where method is defined?
    – sfdc99999
    Feb 3, 2014 at 7:09
  • Did my answer suffice for this question? Or was there something missing in the answer? Nov 16, 2014 at 5:09

1 Answer 1


I have done this before actually.

What I did was created a seperate class that needed the funtionality without the sharing rules. Then I used that class to carry out my functionality.

For example:

 public with sharing class ClassNeedingSharing
     public void MethodWithSomeFunctionality()
        NoSharingClass ExampleTwo = new NoSharingClass();

Then in the opposing class:

public without sharing class NoSharingClass
   public void FunctionWithoutSharing()
      //Logic needed without security or access permissions

   public static void StaticFunctionWithoutSharing()
     //Static method without sharing

The article you mentioned further verifies that statement.

I approve that approach because in times where you do need sharing rules or not, you have an easy way of choosing so.

As far as limit concerns and what-not, I don't believe so (you may have more records with without sharing is the only one I can think of). Honestly, I cannot see a disadvantage of properly structuring your code based on sharing inheritance because it would make a lot of sense in this situation. Just as long as you are careful with the functionality of anything within the without sharing class.

You must log in to answer this question.

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