You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Apex Sharing Best Practices in Salesforce


Salesforce provides standard features for sharing data among users.  They are

  • Organization Wide Defaults
  • Role Hierarchy
  • Public Groups
  • Sharing Rules.
  • Territory Management.

Using these features, you can share data record level.  In many scenarios, you can use these standard functionalities, but there are a few cases in which Apex Managed sharing helps us to share records among users.

Apex Managed Sharing 

Apex managed sharing is a programmatic sharing. To share record using Apex managed sharing, you need to write the Apex code.

Programmatic sharing needs following field values for sharing records.

Fields Description
ParentId The Id of the record being shared. This is read-only
UserorGroupId

 

The Id that we are granting access to. The Id is Role Id (or) User Id (or) Public group Id (or) Territory Id.
AccessLevel Level of Access Read or Edit.
RowCause Reason for why the record is shared to user or group.

 

Apex managed sharing will create records in Share tables with custom RowCause or “Manual” RowCause.   Manual RowCause is User Managed Sharing.

User Managed Sharing 

In User Managed Sharing is, a user who has full access on the record shares the records with others, but when the record owner is changed, this record will be removed from Sharing table.  Similarly, when apex sharing is defined as “Manual” on RowCause, it will remove record from sharing table when the record owner is changed.

To resolve this issue, we need to define Apex Sharing Reason on Rowcause while writing Apex Sharing.

Let’s see how to implement Apex Sharing Reason in below.

Implementing Apex Sharing Reason

Follow the below steps to create Apex Sharing Reason: 

  1. Go to custom object
  1. Click [New] in Apex Sharing Reason Related list.
  1. Enter label and name for the Apex Sharing Reason.

salesforce apex

  1. Hit the [Save] button.

salesforce apex

 Let’s see how to use this Apex Sharing Reason in code.

Example Code: 

what is salesforce apex

Standard Object’s Record Sharing: 

Since we have defined Apex Sharing Reason on Custom Object sharing, it will keep Share table records updated whenever record owner is changed.  So, still granted user can access the records without any issue. This scenario is totally different for standard object because Standard objects don’t support Apex Sharing Reason. So, while sharing standard object records, by default you must define RowCause is “Manual”.

It will impact the sharing table when record owner is changed. To solve this issue, we need to call apex sharing logic for both Insert as well as update function.

Example Apex Sharing Code for Standard Objects: 

apex class in salesforce

Record Sharing via Workflow Rule.

When you are sharing records to external system via Outbound Message, you should add condition like “OwnerId <> PriorValue(OwnerId)”. So, whenever record owner is going to change, it will share the records to external system again.

Summary: 

Apex managed sharing is a dynamic and flexible sharing process.  Sharing records created by Apex code are maintained across record owner changes. Using this Apex managed sharing, you can implement all the complex sharing as it gives you full control over the record sharing.

References:

https://developer.salesforce.com/page/Using_Apex_Managed_Sharing_to_Create_Custom_Record_Sharing_Logic