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
satheesh psatheesh p 

what is the use of with Sharing in Apex ??

Hi all,

public with sharing class SimpleErrorController { }

In the above code what is the with sharing..
Best Answer chosen by satheesh p
Apoorv Saxena 4Apoorv Saxena 4
Hi Satheesh,

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

Please let me know if this helps!

Thanks,
Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Satheesh,

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

Please let me know if this helps!

Thanks,
Apoorv
This was selected as the best answer
Rowallim TechnologyRowallim Technology
Hi Satheesh
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. For more information on executeAnonymous, see Anonymous Blocks.
Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. For example:
 public with sharing class sharingClass
  {
   // Code here
  }
Hope this will help you.
If you think your question is answered mark it as best answer.
Thanks
farukh sk hdfarukh sk hd