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
Yogesh SheteYogesh Shete 

Which sharing keyword should I use on trigger handler class? With Sharing or Without Sharing?


I am writing a trigger handler class for trigger in apex. But I am confused, whether to use 'With Sharing' or 'Without Sharing' keyword on handler class? In apex, trigger runs in system context so trigger handler should run in system context. So to run trigger handler in system context 'Without Sharing' keyword must be used on it. But almost all places, I have seen that 'with sharing' keyword gets used on trigger handler class.

Would anyone help me? Please.
NagendraNagendra (Salesforce Developers) 
Hi Yogesh,

By default, Apex executes in the system context. Apex code has access to all objects and fields. Object permissions, field-level security, and sharing rules aren’t applied for the current user. You can use the with sharing keyword to specify that the sharing rules for the current user be taken into account for a class. This topic is important, so check out Using the with sharing or without sharing Keywords.

For more information please check with below links. Best Regards,

Yogesh SheteYogesh Shete
Thanks Nagendra.

I have read the above provided links and other stuff too. But everywhere 'With Sharing' keyword gets used on trigger handler classes. So my question still remains. Can't we use 'Without Sharing' on trigeer handler classes as it should run in system context as trigger?
Ilan Levi-Mazloum 9Ilan Levi-Mazloum 9
Hi @Yogesh Shete,

I think that 95% of salesforce Developer don't know about the signification of System / User mode and about the signification of With / Without sharing.

Trigger Handlers need to be on System mode.