1

If with sharing or without sharing is not specified in the following, will only the controller run with sharing? I think only apex controller and anonymous block will respect the OWD and sharing settings of the user who is running the code.

  • Apex Controller
  • Anonymous Block
  • Apex Trigger

For Apex Trigger, it doesn't matter whether the with sharing or without sharing is there or not. Triggers will always execute in System Mode.

Please correct if my understanding is wrong.

2 Answers 2

2

Note from Using the with sharing or without sharing Keywords:

The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in system context. In system context, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied for the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user. The only exceptions to this rule are Apex code that is executed with the executeAnonymous call and Chatter in Apex. executeAnonymous always executes using the full permissions of the current user.

So in your first paragraph, you are not quite right. For a class that does not specify a sharing setting, Execute Anonymous will enforce sharing every time. If the class is called from an Apex Trigger, it will run without sharing when not specified. For a controller extension, it will inherit the active sharing setting, which from the StandardController will be with sharing. I believe that if you write a custom Controller, it will have nothing to inherit and will actually run without sharing, but I would have to check.

As for which setting to use on Apex Classes called from an Apex Trigger, the system will enforce the class sharing setting if one is specified:

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.

1
  • Got it. A custom controller without any sharing mentioned, will run in System Mode i.e. quivalent to "without sharing". Trigger will always run in System Mode. Anonymous block will always respect user's sharing settings and OWD. A Controller extension will inhert the sharing settings from standard controller which is "with sharing" until and unless it is specifically mentioned. Commented Nov 30, 2016 at 20:42
0

If "with sharing" or "without sharing" is not mentioned in the apex controller, then current sharing rules remain in effect.

For anonymous block, code runs with Full permission of the user, So, if user does not permission to specific object or field, it will fail, which may not occur to trigger.

2
  • So basically if "with sharing" or "without sharing" is not mentioned, apex controller and anonymous block will run based on current sharing settings of the user and the OWD. Whereas Trigger will always run the System mode. Correct? Commented Nov 30, 2016 at 19:14
  • For trigger and controller, it will be system mode, and system will not check if user has a permission to that object or fields, but in case of anonymous block it respects OWD, but it will also check it user has permission to that objects or fields Commented Nov 30, 2016 at 19:21

You must log in to answer this question.

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