You need to sign in to do that
Don't have an account?

what is the exact difference between with sharing and without sharing? where we can use in real time?
I am new to salesforce. i have some confussion
There have different ways we can sharing the records like
1)OWD
2)Sharing rules
3)Criteria based
4)Manually sharing
5)using apex program
Now my confussion is which case we can use with sharing and without sharing? where we can use in real time?
Please give me the scenarios and let me know what is the extra features it have ?
Thanks in advance
There have different ways we can sharing the records like
1)OWD
2)Sharing rules
3)Criteria based
4)Manually sharing
5)using apex program
Now my confussion is which case we can use with sharing and without sharing? where we can use in real time?
Please give me the scenarios and let me know what is the extra features it have ?
Thanks in advance
With sharing and without sharing is declared in apex classes to indicate if the class will run following the sharing rules of the running user or not. For example, if you have no object permissions to account and your with sharing apex class does updates on accounts, the class will fail to do the update. Normally, if you don't have the with sharing keyword, the apex class will ignore any sharing rules the running user may have.
You can refer to this documentation for more details:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm
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
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.
Please check below post for sharing rule
1) http://amitsalesforce.blogspot.com/2016/02/types-of-sharing-in-salesforce.html
1. Force.com Managed Sharing
Force.com managed sharing involves sharing access granted by Force.com based on record ownership, the role hierarchy, and sharing rules:
2. User Managed Sharing, also known as Manual Sharing
User managed sharing allows the record owner or any user with Full Access to a record to share the record with a user or group of users. This is generally done by an end-user, for a single record. Only the record owner and users above the owner in the role hierarchy are granted Full Access to the record. It is not possible to grant other users Full Access. Users with the “Modify All” object-level permission for the given object or the “Modify All Data” permission can also manually share a record. User managed sharing is removed when the record owner changes or when the access granted in the sharing does not grant additional access beyond the object's organization-wide sharing default access level.
3. Apex Managed Sharing
Apex managed sharing provides developers with the ability to support an application’s particular sharing requirements programmatically through Apex or the SOAP API. This type of sharing is similar to Force.com managed sharing. Only users with “Modify All Data” permission can add or change Apex managed sharing on a record. Apex managed sharing is maintained across record owner changes.
Let us know if this will help you
https://www.sfdc-lightning.com/2018/11/with-sharing-and-without-sharing-in.html
https://www.sfdc-lightning.com/2018/10/owd-in-salesforce.html
When we use with sharing keyword then the Apex code runs in User Context,
i.e all the sharing rules for the current user are taken into account.
When we use without sharing keywords then the Apex code runs in the system context,
Apex code has access to all objects and fields etc and sharing rules aren’t applied for the current user.
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi