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
inbox outbox 7inbox outbox 7 

Could anyone tell me the reason why we use apex sharing?

I am looking for a scenario as to, when we use apex sharing over declarative ways. 
 
Thank you!
Best Answer chosen by inbox outbox 7
Tushar KhorateTushar Khorate
Apex managed sharing must use an Apex sharing reason . Apex sharing reasons are a way for developers to track why they shared a record with a user or group of users. Using multiple Apex sharing reasons simplifies the coding required to make updates and deletions of sharing records.
Manual Sharing
Manual Sharing is used to share the record Manually. For Example, you are working on an Opportunity record and You need help from your colleague. In this case, you can share the record with him/her and take the help.
Manual Sharing is not available for Lightning Experience
Click here for Manual Sharing for Salesforce Lightning.
Apex Managed Sharing
Apex Managed Sharing only works when the OWD is either private or Public Read Only.
Apex Managed Sharing is used to share the records when you can not use Sharing rules.
We do insert the Share Record using Apex trigger/class.
Maintained across ownership changes
Requires “Modify All” permission

For more detail information see this: https://www.youtube.com/watch?v=9ZnMNPBJ5D0&ab_channel=SFDCPanther%2B (https://www.youtube.com/watch?v=9ZnMNPBJ5D0&ab_channel=SFDCPanther%2B)
If you found this helpful then mark it as best answer so other can also see this if they want help.

Thank you

All Answers

PriyaPriya (Salesforce Developers) 

Hi 

Consider this scenario :- 

On Opportunity, you want to give access to record to some users which are in related list.
One way is to manually share the record which will need the interference of opportunity owner. But everyone will love automated solution.
Apex managed sharing provides developers with the ability to support an application’s particular sharing requirements programmatically via Apex code.

For more detail :- 
https://www.jitendrazaa.com/blog/salesforce/apex-based-sharing-in-salesforce/

If this help, please mark it as best answer.

Regards,

Priya Ranjan

inbox outbox 7inbox outbox 7
Priya, 
Can we not achieve the same thing declaratively?
Tushar KhorateTushar Khorate
Apex managed sharing must use an Apex sharing reason . Apex sharing reasons are a way for developers to track why they shared a record with a user or group of users. Using multiple Apex sharing reasons simplifies the coding required to make updates and deletions of sharing records.
Manual Sharing
Manual Sharing is used to share the record Manually. For Example, you are working on an Opportunity record and You need help from your colleague. In this case, you can share the record with him/her and take the help.
Manual Sharing is not available for Lightning Experience
Click here for Manual Sharing for Salesforce Lightning.
Apex Managed Sharing
Apex Managed Sharing only works when the OWD is either private or Public Read Only.
Apex Managed Sharing is used to share the records when you can not use Sharing rules.
We do insert the Share Record using Apex trigger/class.
Maintained across ownership changes
Requires “Modify All” permission

For more detail information see this: https://www.youtube.com/watch?v=9ZnMNPBJ5D0&ab_channel=SFDCPanther%2B (https://www.youtube.com/watch?v=9ZnMNPBJ5D0&ab_channel=SFDCPanther%2B)
If you found this helpful then mark it as best answer so other can also see this if they want help.

Thank you
This was selected as the best answer
Jeanette MyersJeanette Myers
Can someone hlep me to get the code? I am looking for a code of this page I want to make same design for my blog post https://apkmud.com/episode-mod-apk/
Noor fatimaNoor fatima
Apex sharing is a powerful feature in the Salesforce platform that allows developers to programmatically define complex sharing rules and permissions. While declarative ways, such as using the Salesforce sharing settings or sharing rules, provide a convenient and user-friendly approach to managing access to records, there are scenarios where utilizing apex sharing becomes necessary. Here's an example scenario where apex sharing would be preferred over declarative methods providing by currentpackages (https://currentpackages.com/):

Consider a situation where you have a custom object called "Confidential Document" in your Salesforce org. The object contains sensitive information that should only be accessible to specific users based on their roles and a dynamic set of criteria. The access criteria could be based on a combination of record attributes, user attributes, or even data from external systems.

In this case, utilizing apex sharing would be beneficial because:

1. Dynamic Sharing Logic: Apex sharing allows you to implement custom logic to calculate record-level access dynamically. You can incorporate complex business rules, external data lookups, or even integration with external systems to determine who should have access to a particular record. This level of flexibility and customization is not available with declarative sharing settings.

2. Fine-grained Control: Apex sharing enables you to have fine-grained control over record access. You can define sharing rules at a granular level based on various factors, such as record attributes, user attributes, or even custom logic. This level of control may be necessary when the access requirements are more complex and cannot be adequately addressed by declarative sharing settings alone.

3. External Data Considerations: If the access to the "Confidential Document" records depends on data from external systems or APIs, using apex sharing would allow you to retrieve and process that data dynamically. You can incorporate external data sources into your sharing calculations and update sharing settings accordingly, ensuring accurate and up-to-date access control.

4. Integration with Existing Processes: Apex sharing can be seamlessly integrated with existing Apex code or business processes. If you already have custom logic written in Apex that determines record access for other objects, using apex sharing for the "Confidential Document" object ensures consistency and avoids duplicating the access control logic.

It's important to note that apex sharing should be used judiciously and only when necessary, as it introduces custom code complexity and requires proper testing and maintenance. It's recommended to thoroughly assess your requirements and evaluate the trade-offs before opting for apex sharing over declarative methods.