function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Developer BaseDeveloper Base 

With sharing/Without sharing class called from Trigger

Hello,

trigger always runs in system mode.

https://salesforce.stackexchange.com/questions/172060/types-of-execution-system-mode-or-user-mode

But what if the trigger calls a method from a class which has "with sharing" or "without sharing defined"?

I did my own tests and it seems that if the method is called from a trigger, even the method executes in system mode, so it does not matter if it is "with" or "without sharing", once it is called from a trigger, it always runs in system mode.

Yes or no?
Best Answer chosen by Developer Base
mukesh guptamukesh gupta
Hi,

You are right 

Please have a look on below points:-
  1. If we do not declare class with "With sharing" or "Without sharing"  the class will not take into account the sharing rules but if this class is called from another class which is declared with "With sharing" it will take into account the sharing rules.
  2. IF the class with "With sharing" is calling method of another class with "Without sharing" than the method inside "Without sharing" class will execute without sharing rules.
  3. IF the class with "Without sharing" is calling method of another class with "With sharing" than the method inside "With sharing" class will execute with sharing rules.
  4. Inner class do not have sharing settings of parent class.
  5. Class have sharing settings of parent class when we implements it or extends it from parent class.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

All Answers

mukesh guptamukesh gupta
Hi,

You are right 

Please have a look on below points:-
  1. If we do not declare class with "With sharing" or "Without sharing"  the class will not take into account the sharing rules but if this class is called from another class which is declared with "With sharing" it will take into account the sharing rules.
  2. IF the class with "With sharing" is calling method of another class with "Without sharing" than the method inside "Without sharing" class will execute without sharing rules.
  3. IF the class with "Without sharing" is calling method of another class with "With sharing" than the method inside "With sharing" class will execute with sharing rules.
  4. Inner class do not have sharing settings of parent class.
  5. Class have sharing settings of parent class when we implements it or extends it from parent class.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
This was selected as the best answer
Developer BaseDeveloper Base
Hello Mukesh,

so regarding triggers it does not make any sense to put "with sharing" or "without sharing" in the class methods whih are called from the trigger, yes? They will always run in system mode like the trigger does.

And second question: When does it make sense to use "With sharing" or "without sharing" ? For example when using custom controllers, custom buttons and so on? 

Thanks