Apex – How to Query Salesforce Object Sharing Settings

We have data in our in-house systems relating to Salesforce object records. We need to replicate the Salesforce permissions on the corresponding records in our systems.

I've been investigating retrieving the org-wide default sharing settings through Apex and am having the following issues:

  • I can get the default access for internal users for most standard objects from the Organization object; however, I cannot find a value for the Order object.
  • I cannot find the default access for external users for any object.
  • I cannot find the status of the check boxes (e.g., grant access to hierarchies, portal user visibility, etc.).
  • I cannot find where the configuration for the object sharing rules is stored.

I've checked the ObjectPermissions object and the CustomObject__Share objects, but those object don't hold the above information.

Can anyone point me to where I can query this information?

Best Answer

What you're looking for is the retrieve() call, which lets you specify metadata to get information you're looking for, including the org wide defaults for a given object, grant access via hierarchy options, etc. Note that some objects don't have sharing; they are controlled by the parent object(s) instead. For most of what you want, you will retrieve CustomObject, which also returns standard objects. If you also want Field Level Security, you must also retrieve Profile and PermissionSet.

Related Topic