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.

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

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

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

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
Thanks a lot for this doc!!
LikeLike