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
Muni12345Muni12345 

what is the exact difference between apex managed sharing and withsharing?

Hi,
what is the exact difference between apex managed sharing and withsharing, which case we can use apex managed sharing and withsharing?

please clear my confussion

Thanks in advance
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for with and without sharing
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm

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
public with sharing class sharingClass {

// Code here

}
Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example, you may want to explicitly turn off sharing rule enforcement when a class acquires sharing rules when it is called from another class that is declared using with sharing.
public without sharing class noSharing {

// Code here

}


Let us know if this will help you