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.
Best Answer
Note from Using the with sharing or without sharing Keywords:
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 anApex Trigger
, it will runwithout sharing
when not specified. For a controller extension, it will inherit the active sharing setting, which from theStandardController
will bewith sharing
. I believe that if you write a customController
, it will have nothing to inherit and will actually runwithout sharing
, but I would have to check.As for which setting to use on
Apex Classes
called from anApex Trigger
, the system will enforce the class sharing setting if one is specified: