Activity Forums Salesforce® Discussions How to create share table record using Salesforce apex?

  • Manpreet

    April 27, 2018 at 11:15 am

    Hi pranav,

    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. System only allows insert or delete of records for share table. So if you have to change access for user, you have create new record and can delete already existing record if you want.


  • Aman

    May 1, 2018 at 7:26 am

    Hi Pranav,

    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. In addition, all custom object sharing objects are named as follows, where MyCustomObject is the name of the custom object:


    Objects on the detail side of a master-detail relationship do not have an associated sharing object. The detail record’s access is determined by the master’s sharing object and the relationship’s sharing setting. For more information, see “Custom Object Security” in the Salesforce online help.

    A share object includes records supporting all three types of sharing: managed sharing, user managed sharing, and Apex managed sharing. Sharing granted to users implicitly through organization-wide defaults, the role hierarchy, and permissions such as the “View All” and “Modify All” permissions for the given object, “View All Data,” and “Modify All Data” are not tracked with this object.

    Every share object has the following properties:

    Property Name
    The level of access that the specified user or group has been granted for a share sObject. The name of the property is AccessLevel appended to the object name. For example, the property name for LeadShare object is LeadShareAccessLevel. Valid values are:Edit
    The All access level can only be used by managed sharing.
    This field must be set to an access level that is higher than the organization’s default access level for the parent object. For more information, see Understanding Sharing.
    The ID of the object. This field cannot be updated.
    The reason why the user or group is being granted access. The reason determines the type of sharing, which controls who can alter the sharing record. This field cannot be updated.
    The user or group IDs to which you are granting access. A group can bea public group or a sharing group associated with a role
    a territory group if you use the original version of Territory Management, but not with Enterprise Territory Management
    This field cannot be updated.


Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos