function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Galeeb SKGaleeb SK 

Sharing using Apex

Hi , i am  try to share the records using ''apex sharing '' but sharing can't be possible .please guide me how to share the records using Apex with one Example
Galeeb SK
Amit Chaudhary 8Amit Chaudhary 8
Please check below post that will help you

Example: On Opportunity, you want to give access to record to some users which are in related list.
One way is to manually share the record which will need the interference of opportunity owner. But everyone will love automated solution.

Apex managed sharing provides developers with the ability to support an application’s particular sharing requirements programmatically via Apex code.
Sharing Table

All objects that have a default sharing setting of the either “Private” or “Public Read Only” also have a related “Share” object that is similar to an access control list (ACL) found in other platforms. All share objects for custom objects are named as MyCustomObject__Share, where MyCustomObject__c is the name of the related custom object. A share object includes records supporting all three types of sharing: managed sharing, user managed sharing, and Apex managed sharing.
trigger Test_Share on Test__c (after insert) {


 * Test_Share is the "Share" table that was created when the Organization Wide Default
*  sharing setting was set to "Private". Allocate storage for a list of Test_Share
* records.
 List<Test_Share> jobShares = new List<Test_Share>();

 /** For each of the Test records being inserted, do the following: **/
 for(Test__c t :{

 /** Create a new Test_Share record to be inserted in to the Test_Share table. **/
 Test_Share studentRecord = new Test_Share();

 /** Populate the Test_Share record with the ID of the record to be shared. **/
 studentRecord.ParentId = t.Id;

 /** Then, set the ID of user or group being granted access. In this case,
 * we're setting the Id of the student__c that was specified by the Test
 * Result in the student__c lookup field on the Test record.

 studentRecord.UserOrGroupId = t.student__c;

 /** Specify that the Student should have edit access for this particular Test record. **/
 studentRecord.AccessLevel = 'edit';

 /** Specify that the reason the Student can edit the record is because its his test result
 * (Student_Access__c is the Apex Sharing Reason that we defined earlier.)
 studentRecord.RowCause = Schema.Test_Share.RowCause.Student_Access__c;

 /** Add the new Share record to the list of new Share records. **/

 /** Insert all of the newly created Share records and capture save result **/
 Database.SaveResult[] jobShareInsertResult = Database.insert(jobShares,false);
Please let us know if this will help you

Amit Chaudhary
Hi Galeeb,

Apex Sharing :
Providing Sobject Access to targeted user by using Apex is called Apex Sharing.
By Default if OWD of the object is not set to most permissive access level(Public Read/Write) ,Then there will be automatically share object created for the existing object .
For Example if OWD for 'Account' is not set to Public Read/Write then 'AccountShare' object will be existing in the back-ground .

Please refer below post which can help you understand with simple real time scenioro 

Apex Managed Sharing Explained with real time examples and use cases (