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
Olivia Porter 1Olivia Porter 1 

With Sharing and Without Sharing Rules

This may be difficult to type out but I am confused on the difference between using "without sharing" and just not declaring any sharing rules at all.

Can you only declare conditions that allow a user to see parts of the salesforce org? For example: if I wanted to block someone from seeing any Account in the org, would that be a part of the sharing rules?

If so, then I really don't get the difference between "without sharing" and just simply not declaring any sharing rules for the class.
Best Answer chosen by Olivia Porter 1
Apoorv Saxena 4Apoorv Saxena 4
Hi Olivia,

I'll try to make things clear for you.

When you use 'with sharing'(with Security Settings enforced) keyword the Apex code runs in User Context, i.e all the sharing rules for the current user are taken into account.

When you use 'without sharing' keyword the Apex code runs in 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.

With Sharing:

Let's say you're trying to perform an update as the user A.Now If user A cannot edit record R1, then an error will occur. If user A cannot see record R1, they cannot query that record. Use this mode when for pages that should respect sharing settings, or any other time you might want to enforce the rules.

Without Sharing:

This is when you're trying to perform an update that should not fail ordinarily.

For more info on this refer to the following link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm)

Please let me know if this helps!

Thanks,
Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Olivia,

I'll try to make things clear for you.

When you use 'with sharing'(with Security Settings enforced) keyword the Apex code runs in User Context, i.e all the sharing rules for the current user are taken into account.

When you use 'without sharing' keyword the Apex code runs in 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.

With Sharing:

Let's say you're trying to perform an update as the user A.Now If user A cannot edit record R1, then an error will occur. If user A cannot see record R1, they cannot query that record. Use this mode when for pages that should respect sharing settings, or any other time you might want to enforce the rules.

Without Sharing:

This is when you're trying to perform an update that should not fail ordinarily.

For more info on this refer to the following link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm)

Please let me know if this helps!

Thanks,
Apoorv
This was selected as the best answer
farukh sk hdfarukh sk hd