SOQL Relationship Query / Relationship Names for Custom Object (SObject) Share Table

I haven’t posted anything to this blog in years.  I’ve never been much of a blogger, or good at maintaining my “digital presence.”  But, I just had another one of those moments I think all us Salesforce architects / developers / admins / etc. have semi-frequently in which I searched the Internet and read results for hours for something I thought would be easy to find, but couldn’t find it.  So, here goes a write up.

My goal was to keep my SOQL query count lean in a context in which I needed to get a list of custom object records, and all the shares for each of those custom object records.  I thought to myself, “I can probably do this with a relationship query into a list of the custom object type.  I’ll just have to figure out what the relationship name is for the lookup field on the custom object’s sharing table.

Spoiler for those who just want the answer:  “Shares” is the relationship name you’re looking for.

I’ve been working with the Apttus CPQ and CLM tool sets of late.  To get specific, I was trying to query up Apttus Agreements and their related shares.  Now, what I learned here will apply for ANY custom object / sObject in Salesforce, but what follows here is going to reference an Apttus object / namespace.  I started out trying something like this:

List myAgrList = [ SELECT Id, OwnerId, (SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause FROM Apttus__APTS_Agreement__Shares) FROM Apttus__APTS_Agreement__c ];

That wasn’t right.  I tried making the relationship name “Apttus__APTS_Agreement__Share” because I thought I could remember some standard objects not following the plural convention.  I tried a lot of other things, and looked in a lot of wrong places.   I asked myself if it had something to do with this being a managed package object and the related namespace. I tried “Parents” and “Parent” since that was the relationship name I saw for the ParentId field on the sharing object.  I

Finally, it was going into Workbench and looking at a relationship I know best…Accounts and Contacts…that led me to the answer.  I looked at the Contact table definition in Workbench, and saw the “Child Relationship” folder.  I saw there was a sub-folder called “ContactShare.ContactId.”  In that folder, I saw the relationshipName value:  Shares

So, I went to the Apttus__APTS_Agreement__c object in Workbench.  I went to the “Child Relatiohships” folder and found a sub-folder called Apttus__APTS_Agreement__Share.ParentId.  In that folder, I was the “relationshipName” attribute also has the value:  Shares

So, there you go.  Hope it helps somebody else.  If you want to write a SOQL relationship query for Share table children, for a standard or custom object, with namespaces or without, try relatiohshipName:  Share

Leave a comment