Sharing records by Apex in Salesforce

by Rijwan Mohmmed

Hello friends, today we will learn how to share records in apex class. We all know that we can share records by multiple ways.

Key Highlights :

  1. We share the records because when the OWD for object records is private.
  2. We use sharing setting when there are some criteria that they meet we will share records to groups/roles.
  3. But when there is very complex logic we use manual sharing but it will helpful for some records, if there are many records and we want it’s we do automatic then we use Apex sharing.

Salesforce provides share object for every object like

  1. The standard object has “StandardobjectName+Share”, eg. Account has AccountShare.
  2. Custom object have “CustomObjectName__Share” , eg. Custom_Object__c have Custom_Object__Share.

Share Custom Object Fields

  1. ParentId: is the Id of the record that you want to share.
  2. UserOrGroupId: is the Id of the user or public group or roles with whom you want to share the record.
  3. AccessLevel: is the level of access that you want to give to that user or group. It can be Read, Edit or All. All is given only by system so you can only view if permission is given you cannot assign to any user or group through code.
  4. RowCause: is the reason which can be helpful when reading the shared record’s reason.

Apex sharing reason :

Before we can start writing any Apex-managed sharing code, we must create an Apex-sharing reason. To do that:

  • Click Setup > Create > Objects.
  • Select the custom object. (In this case, the “Test” Custom object.)
  • Click New in the Apex Sharing Reasons-related list.
  • Enter a label for the Apex sharing reason.
  • Enter a name for the Apex sharing reason.
  • Click Save.

Code :

  1. Sharing a standard object record
public class ShareStandardObjectRecord{
	public static void shareRecord(Id accountId){
		AccountShare Share_Record = new AccountShare();
		/* Object Record Id- Refer to standard share objects fields */
		Share_Record.AccountId = accountId;
		Share_Record.UserOrGroupId = '0059000000239wXAAQ'; //user or group Id whom to share record
		Share_Record.AccountAccessLevel = 'Edit';
		Share_Record.OpportunityAccessLevel = 'Read';// these only for account object
		Share_Record.RowCause = Schema.AccountShare.RowCause.Manual; // reason
		insert Share_Record;

2. Sharing a custom object record

public class ShareCustomObjectRecord{
	public static void shareRecord(Id recordId){
		MyCustomObject__Share Share_Record = new MyCustomObject__Share();
		/* Object Record Id- Refer to custom share objects fields */
		Share_Record.parentId = recordId;
		Share_Record.UserOrGroupId = '0059000000239wXAAQ'; //user or group Id whom to share record
		Share_Record.AccessLevel = 'Edit';
		/** Specify that the reason the user can edit the record is because its his custom object result
         * (User_Access__c is the Apex Sharing Reason that we defined earlier.)
        Share_Record.RowCause = Schema.MyCustomObject__Share.RowCause.User_Access__c;
		insert Share_Record;
What’s your Reaction?

You may also like

Leave a Comment