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 Apex managed sharing? How its work and which case we can use?

I am new to salesforce, I have some confussion with Apex managed sharing, where we can use exactly in real time?

Please clear my confussion..

Thanks in advance
brahmaji tammanabrahmaji tammana
Hi Vasu,

As we know, we have different ways of sharing a record in Salesforce. One of the ways is Apex Managed Sharing which is a bit adavanced compared to others and has to be builit programmatically through Apex or Soap API.

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.

Basically, if you want to share a record programmatically we use Apex Managed Sharing.
For example, AccountShare is the sharing object for the Account object, ContactShare is the sharing object for the Contact object, and so on. In addition, all custom object sharing objects are named as follows, where MyCustomObject is the name of the custom object:
MyCustomObject__Share

You can find more details in Salesforce Documentation which can be accessed via below link.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm


If this answer helps you, do not forget to mark it as Best Answer.

Thanks
Brahma
GauravGargGauravGarg

Hi Vasu,
 

Sharing word comes when we need to share a record with another user / group / queue on a particular condition. We can share records on various parameters:
Record Ownership
Role Hierarchy
Territory Hierarchy: 
Teams: Account Team, Opportunity Team, Sales Team, Case team based sharing.
Sharing rules: these are defined under sharing settings.

To get more information on this please go through : https://developer.salesforce.com/page/Using_Apex_Managed_Sharing_to_Create_Custom_Record_Sharing_Logic

Hope this will clear your confusion. 

Thanks,

Gaurav

Muni12345Muni12345
Hi Brahma,

Thank you for your response, still i am in confussion

For share the record there have multiple ways right like
1)OWD
2)sharing rules
3)role hierarchy
3)Criteria based
4)Manual sharing
now my doubt is we have these many option for share the records, what is the additional feature it have with apex managed sharing,
why we are using this? and which scenario we should use?

Please clear my confussion

Thanks in advance
Vasu  
GauravGargGauravGarg

Hi Vasu,

Please find below details for above features:
1. OWD: it is used to set Object sharing for internal and external person that will be applicable to all user's irrespective of role / profile. 
2. Sharing Rules: these are defined for objects with Private, to share record with Roles / Groups / users. 
3. Role Hierarchy: Basically used in Public Groups to share records with user's in above hierarchy.
4. Criteria Based Sharing: Quite similar with sharing rules, just need extra feature to share records based on particular conditions. 
5. Manual Sharing: Owner or user's having sharing permission can share records with particular person /groups

Now, Apex sharing is used when we need to share records which are not inter-linked or Lookup relations or  user's are in down hierarchy. So we create apex class that will create sharing records for such users.

Hope this helps!!!

Thanks,

Gaurav
Skype: gaurav62990

F SmoakF Smoak
Hi Gaurav,

I am stuck in a similar problem, need your kind hep here.

I have calls which is related to account which are created by different users in org. Now I have a standalone custom object alerts. My req is to create alerts for calls created in past 7 days and the alerts should be visibile to all the users who have access to accounts which maybe common. I have created a batch but created date becomes admin id so alerts are not visible to end users as OWD is private for security reaso. Kindly let me know how to provide visibility to all the alerts for which accounts are available to users

 
F SmoakF Smoak
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000XkdDQAS

My Question posted.
GauravGargGauravGarg
Hi, 
There are a few options:
1. Change the owner to Group /Queue/user for the alert. 
2.  Create a report and share with a group of user
3. Create a component, that is visible to all user and place the Alerts records in the component. Run the component based on System Admin. 

Thanks,
Gaurav
Skype: gaurav62990
raj_sfdccraj_sfdcc
Hi Muni,

Reason For Usage Of Apex Sharing :
The client might required to share the record access to different set of users based on COMPLEX CRITERIA/CONDITIONS  which cannot be done using sales force native features like Sharing Rules/OWD.

Apex Sharing :
Providing Sobject Access to targeted user by using Apex is called Apex Sharing.
By Default if OWD of the object is not set to most permissive access level(Public Read/Write) ,Then there will be automatically share object created for the existing object .
For Example if OWD for 'Account' is not set to Public Read/Write then 'AccountShare' object will be existing in the back-ground .

Please refer below post which can help you understand with simple real time scenioro 

Click Here for Apex Sharing Explanation (https://salessforcehacks.blogspot.com/2020/01/apex-managed-sharing-with-real-time.html)