Salesforce Sharing and Visibility Designer Series Part 12

This entry is part 13 of 17 in the series Sharing and Visibility Designer Study Guide

Sharing a Record Using Apex

  • To access sharing programmatically, you must use the share object associated with the standard or custom object for which you want to share. For example, AccountShare is the sharing object for the Account object, ContactShare is the sharing object for the Contact object. In addition, all custom object sharing objects are named as follows, where MyCustomObject is the name of the custom object: MyCustomObject__Share
  • Objects on the detail side of a master-detail relationship do not have an associated sharing object. The detail record’s access is determined by the master’s sharing object and the relationship’s sharing setting.
  • A share object includes records supporting all three types of sharing: managed sharing, user managed sharing, and Apex managed sharing. Sharing granted to users implicitly through organization-wide defaults, the role hierarchy, and permissions such as the “View All” and “Modify All” permissions for the given object, “View All Data,” and “Modify All Data” are not tracked with this object.
  • Every share object has the following properties:
    • objectNameAccessLevel
    • ParentID
    • RowCause
    • UserOrGroupId

It is possible to manually share a record to a user or a group using Apex or the SOAP API. If the owner of the record changes, the sharing is automatically deleted.

Manual shares written using Apex contains RowCause=”Manual” by default. Only shares with this condition are removed when ownership changes.

Sample Code:

How Apex Managed Sharing Works ?

  • Apex managed sharing enables developers to programmatically manipulate sharing to support their application’s behavior through Apex or the SOAP API. This type of sharing is similar to 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.
  • 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. They also enable developers to share with the same user or group multiple times using different reasons.
  • Apex sharing reasons are defined on an object’s detail page. Each Apex sharing reason has a label and a name:
  • The label displays in the Reason column when viewing the sharing for a record in the user interface. This label allows users and administrators to understand the source of the sharing. The label is also enabled for translation through the Translation Workbench.
  • The name is used when referencing the reason in the API and Apex.
  • An Apex sharing reason is simply a row cause that you can define for yourself, then use in your code when you write rows in the sharing tables.

  • Because your row cause for these shares will no longer be ‘manual’, the platform won’t touch them when performing the change owner operation. In addition, you can create multiple sharing reasons for different purposes—for the same object. This capability can help you track why you wrote a particular share, and control in your code when and why that share should be deleted or updated in some way.
Series Navigation<< How to prepare for Salesforce Sharing and Visibility Designer certificationSalesforce Sharing and Visibility Designer Series Part 13 >>
Posted in Contributors and tagged , .

Leave a Reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of