How to share records using apex in salesforce


To create an Apex sharing reason:
  1. From the management settings for the custom object, click New in the Apex Sharing Reasons related list.
  2. Enter a label for the Apex sharing reason. …
  3. Enter a name for the Apex sharing reason. …
  4. Click Save.


Can you share reason in Salesforce apex?

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.

How to share a test_share record in Salesforce apex?

Now let’s start with Apex code: * sharing setting was set to “Private”. Allocate storage for a list of Test_Share * records. /** Create a new Test_Share record to be inserted in to the Test_Share table. **/ /** Populate the Test_Share record with the ID of the record to be shared.

How to share records in Salesforce with other users?

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. Custom sharing reason in custom object share. Custom sharing records.

What is manual share in Salesforce apex?

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.


How do I share records on Apex sharing?

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.

What is Apex sharing in Salesforce?

Sharing is the act of granting a user or group of users permission to perform a set of actions on a record or set of records. Sharing access can be granted using the Salesforce user interface and Lightning Platform, or programmatically using Apex.

What are the different ways of sharing records in Salesforce?

What are types of sharing rules in salesforce? Managed Sharing:- … Record Ownership. … Role Hierarchy. … Sharing Rules. … User Managed Sharing, also known as Manual Sharing. … Apex Managed Sharing.

What is Apex sharing recalculation in Salesforce?

Salesforce automatically recalculates sharing for all records on an object when its organization-wide sharing default access level changes. The recalculation includes access granted by sharing rules. In addition, all types of sharing are removed if the access they grant is redundant.

What do Apex sharing rules accomplish?

Therefore, it’s crucial that you enforce sharing rules, set object and field permissions, and protect against CRUD and FLS. Apex sharing rules are used to determine the “execution context” under which your code executes.

What is with sharing and without sharing in Apex class?

The sharing setting of the class where a method is defined is applied, not of the class where the method is called from. For example, if a method is defined in a class declared as with sharing is called by a class declared as without sharing , the method executes with sharing rules enforced.

Which two methods can be used to share records using sharing rule?

There are basically two types of sharing rules in Salesforce based on which records should be shared: Owner-based Sharing Rules. Criteria-based Sharing Rules.

How do I share data in Salesforce?

Salesforce provides many flexible options for you to control how records are shared within your org. To specify the objects and tabs that a user can access, assign a profile. To specify the individual records that a user can view and edit, set your org-wide defaults, define a role hierarchy, and create sharing rules.

How do I share a record in Salesforce lightning?

To share records with Manual sharing in Lightning, first, go to the Record page and click on Sharing button. A Share popup will open where can select and search either User, Public Group, Role and Role and Subordinates. And we can also provide the Read-Only or Read/Write access.

What is share record in Salesforce?

Salesforce Manual Sharing allows the users to share the record to users who would not have access to the record any other way. Only these 4 users can share the record: Record Owner. A user in a role above the owner in the role hierarchy. Users granted “Full Access” to record.

What is sharing object in Salesforce?

A share object includes records supporting all three types of sharing: managed sharing. User managed sharing. Apex managed sharing.

Can we create Apex sharing reason for standard object?

No. We can define apex sharing reasons for custom object. Apex sharing reasons are defined object by object. – so, positions might have different reasons than candidates.

How to access sharing programmatically?

To access sharing programmatically, you must use the share object associated with the standard or custom object for which you want to share. For example, AccountShare is the sharing object for the Account object, ContactShare is the sharing object for the Contact object, and so on.

Is Apex Sharing Reason available for standard objects?

As Apex Sharing Reason not available for Standard objects only way to create Apex based sharing for Standard objects are using row cause Manual. As row cause is Manual for Standard objects, Apex based sharing would be lost once owner is changed [Because its behavior of Manual Sharing] In case of custom object & custom Apex Sharing Reason, …


Share table contains four columns: ParentId, UserOrGroupId, RowCause, AccessLevel.
Below is code to create position__share record:
Position__share p = new Position__share ();
p.parentId = ‘Position Record Id which needs to be shared’;
p.userOrGroupId= ‘User id or Group id with which we want to share record’;
p.RowCause=’ apex sharing reason defined for custom object, here for position__c’;
p.AccessLevel =’access level for record’; //can be Read or Edit
insert p;
Note: you cannot update share table record.


To access sharing programmatically, you must use the share object associated with the standard or custom object for which you want to share. For example, AccountShare is the sharing object for the Account object, ContactShare is the sharing object for the Contact object.


Leave a Comment