Salesforce Basic · Security Model

Apex Sharing Reason in Salesforce

When you want to share a record through apex sharing you can provide custom reason so that it will give a meaningful reason to why the record is shared with a specific user or group.

Custom sharing reason is only available for custom objects and you can set custom sharing reason in salesforce classic version only.


How to create a custom sharing reason.

Step 1: Login to salesforce and switch to classic version of salesforce if you are in lightning version. Also go to setup.

Step 2: In quick search type ‘Objects’. Select ‘Objects’.

Step 3: go to your custom object record where you want to create a new Sharing Reason.

A screenshot of a computer

Description automatically generated
Custom object detail page

Step 4: scroll down to Apex Sharing Reason section or click on the Apex sharing reason link as shown in above image.

Sharing reason section of custom object.

Step 5: Click new.

Step 6: Enter specific label to the sharing reason. Reason Name will be autogenerated. Click Save.

A screenshot of a social media post

Description automatically generated
Create New Sharing Reason.

You can see that a new Sharing reason is created for the custom object.

List of all sharing reasons.

You can use the sharing reason in your apex sharing RowCause. You can use the same as mentioned Below.

  • If you give ‘New Sharing Example’ as label.
  • Its name will be ‘New_Sharing_Example’.
  • You can use it in RowCause as ‘New_Sharing_Example__c’
/*  Mention sharing reason, If this field is missed then by default Manual will be set as reason */
a_Share_Record.RowCause = Schema.MyCustomObject__Share.RowCause.New_Sharing_Example__c;

Note:

  • Only users with the “Modify All Data” permission can add, edit, or delete sharing that uses an Apex sharing reason.
  • Deleting an Apex sharing reason will delete all sharing on the object that uses the reason.
  • You can create up to 10 Apex sharing reasons per custom object.
  • You can create Apex sharing reasons using the Metadata API.
  • It is important to give proper meaningful label so that if you look at the reason of share, it will give a proper message on why and with whom these records are shared.

Resource

Salesforce – Create Apex Sharing Reasons

One thought on “Apex Sharing Reason in Salesforce

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s