SELECT Id, ParentId FROM sObject__Share where UserOrGroupId = :UserId

3 Answers 3


Following query works for me fetching from <your_customobject>__share and RowCause will give you the reason of sharing

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.

SELECT Id, UserOrGroupId, RowCause FROM Test__share WHERE ParentId = 'a1Cq0000003d58H'


For standard object like Case, you need to fetch from CaseShare object

SELECT Id, UserOrGroupId, RowCause FROM Caseshare WHERE CaseId = '500q0000003Q38g'

case share

For more information, refer Sharing a Record Using Apex and CaseShare

  • 1
    Case in point with respect to my comment on @AndrewMayes answer. :)
    – Dan Jones
    May 15, 2017 at 16:41

This actually works. My problem was with a null pointer because of a related record that was not available when I load the page. This even worked for when there was manual sharing.

  • 1
    Welcome to SFSE! Please take a moment to visit the help center, scroll through the tour, and read How to Ask. Remember to include the actual problem you have in the question, specifically the null pointer part. The query you've written is actually correct so in which case no one would be able to help you because there's nothing to help with!
    – Dan Jones
    May 15, 2017 at 16:38
  • 1
    Thanks Dan, I realized this after the fact. I was trying to be specific on the question but I will take a look. May 15, 2017 at 17:29

I don't think you can query "sObject__Share" like that. You will have to query each object individually.

Try using something like Schema.getGlobalDescribe(); to get the list of all sObjects. You can then parse through the result, going object by object and running your Query for each one. (For orgs will lots of objects, be wary of hitting any query limits)

  • 2
    I think he's using sObject__Share as a general term to avoid using the API name of a real object that would typically make less sense to us outside his Org. Querying based on something like Schema.getGlobalDescribe() would rapidly smash limits and simply wouldn't be sustainable as an org grows, as you correctly identify through query limits. Additionally, I'd expect you to rapidly hit collection size limits too.
    – Dan Jones
    May 15, 2017 at 16:35
  • Agreed Dan, hence my bit about limits in brackets. I don't think it's sustainable for a large enterprise org, but is plenty fine for small orgs that would never be close to these limits. Also, getGlobalDescribe would surely constitue only one call. Thereafter, there will be as many calls as there are sobjects. If doing this inside apex, I can't think of a simple way to otherwise do this. @AndrewMayes , if this works as you say, can you please mark as such. Alternatively, I would be interested if anyone had any less potentially-limit-killing mechanisms May 15, 2017 at 16:54
  • You're right. If you absolutely had to, you'd need to put restrictions in place akin to Valdiation Rules (e.g. "You may only select up to 5 objects", "Showing 1000 records of x", that type of thing).
    – Dan Jones
    May 15, 2017 at 16:59
  • It was pseudo code. I know that lol May 15, 2017 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .