SOQL Query on Sharing Objects

Salesforce has a highly advanced security model, and we can specify at the record level, who has access to any record. The access for each record is stored in that record’s sharing object. And SOQL query can help us to fetch this information.

Query on a standard object’s share records:

SELECT Id, AccountId, UserorGroupId, AccountAccessLevel, RowCause
FROM AccountShare
WHERE AccountId = '001frgthyjugggt001'

Query on a custom object’s share records:

SELECT Id, AccountId, UserorGroupId, AccountAccessLevel, RowCause
FROM Clinic__Share
WHERE ParentId = 'a0K0o00000o7gm5'
Important
Note that for Account the Share Object is AccountShare, whereas for Clinic__c, it is Clinic__Share.

Sahre Object fields:

  • ParentId: The ID of the record being shared.
  • UserorGroupId: The Id of the user, or the public group with which the record is being shared.
  • AccessLevel: Read/Edit.
  • RowCause: The reason why the record is shared with that userOrGroupId.