Salesforce Admin · Salesforce Basic · Security Model

Apex Sharing in Salesforce

There will be some scenarios in organization where you want to share a record based on some complex scenario, but you cannot define the complex scenario as a criterion in sharing rule. Then the question is how you can share a record, of course you can share them manually. But how many times you can share records manually, trust me its annoying to share records manually again and again.

Salesforce provides another way to share a record and that is through apex code via share object. So what is Share object? Every standard and custom object will have a share object in salesforce with predefined fields as shown below.

  • Standard Object’s Share object will be “sObject+Share”. For example, Account objects Share Object is AccountShare. Similarly, Opportunity objects Share object will be OpportunityShare.
  • Custom Object’s Share object will be “CustomObjectName + __Share”. For example, if your custom object name is MyCustomObject, then its API name will be MyCustomObject__c and share object will be MyCustomObject__Share.

Where can you see share object in salesforce?

You can see the object and its fields from your developer Console.

Step 1: login into salesforce and open Developer Console.

A screenshot of a cell phone

Description automatically generated
Home page of salesforce

Step 2: in developer console, Click on File – Open (Or Press Ctrl+O).

Open window in developer console

Step 3: In open screen, under Entity Type select “Objects” and in filter the repository section type name of object (Standard or custom). Once you see Share object in entity, double click on the share object.

A screenshot of a social media post

Description automatically generated
Share object
A screenshot of a cell phone

Description automatically generated
Share object fields.

Here,

ParentId is the Id of the record which you want to share.

UserOrGroupId is the Id of the user or public group or roles with whom you want to share the record.

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.

RowCause is the reason which can be helpful when reading the shared record’s reason. Which gives a glimpse of why the record is shared with specific group or user.

Note: Apex Sharing Reason is only available in Classic version of salesforce.

All other fields are system generated.


How can you share record?

Sample Code: To share a Custom object record.

I have created a Sharing Reason. You can create a custom sharing rule reason or use “Manual” as reason while writing code, its default from salesforce.

Know more about Sharing Reason

MyCustomObject__Share a_Share_Record = new MyCustomObject__Share();

/* Access Level (‘Read’ or ‘Edit’ only cannot mention ‘All’) */
a_Share_Record.AccessLevel = 'Edit';

/* UserId or Public Group Id
 or RoleId  */
a_Share_Record.UserOrGroupId = '0059000000239wXAAQ';

/*  ParentId is the id of record which is to be shared */
a_Share_Record.ParentID = 'a0i2v00000UXKVu';

/*  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_Reason_Example__c;

/*  Insert the share object record */
Insert a_Share_Record;

You can try executing in anonymous window. Once a record is inserted, you can go to the record that you just shared and click on sharing button – Expand list (Salesforce Classic version only). You will see the user or group name and you can observe the Reason section.

A screenshot of a cell phone

Description automatically generated
Custom sharing reason in custom object share. Custom sharing records.

The above example is for custom object.


Let

us see an example of sharing a standard object record.

/* Standard share object */
AccountShare a_Share_Record = new AccountShare ();

/* UserId or Public Group Id
 */
a_Share_Record.UserOrGroupId = '0059000000239wXAAQ' ;

/* Object Record Id- Refer to standard share objects fields */
a_Share_Record.AccountId = '00190000027N6pN';

/* Record Sharing Reason
 can only be Manual for Standard share object, you cannot mention custom share reason. Even if this line is skipped salesforce will assign Manual as share reason. */
a_Share_Record.RowCause = Schema.AccountShare.RowCause.Manual;

/* Access Level (‘Read’ or ‘Edit’ only cannot mention ‘All’) */
a_Share_Record.AccountAccessLevel = 'Edit';
a_Share_Record.OpportunityAccessLevel = 'Read';  // Only when sharing a account.
a_Share_Record.CaseAccessLevel = 'Read';         // Only when sharing a account.

/* Insert a share object record */
Insert a_Share_Record;

If you observe the additional fields in account sharing i.e Opportunity access level and Case access level those are required field setting that user must mention.

For other standard objects there will be only one access user must provide that too for parentid

A screenshot of a social media post

Description automatically generated
Account Sharing records.

Note:

  • You cannot mention a custom reason for a standard object.
  • Custom reasons can be created for custom objects only and that too in their respective object setting section.
  • Please check Share object field names (especially parentId field) for standard objects before creating a share record.
  • Manual shares written using Apex contains RowCause=”Manual” by default.
  • Shares with ‘Manual’ as RowCause (Reason) condition are removed when ownership changes.
  • You can create Share object records from Triggers, normal apex class, Flows etc.

 Resource:

Create Apex Sharing Record

Sharing a record using Apex

3 thoughts on “Apex Sharing in Salesforce

    1. This is at record level. if you are looking at list view it is a different scenario. You need to go with some custom code and global action for that. Let me know if you need code for the same.

      Like

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 )

Facebook photo

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

Connecting to %s