2

Is it possible to get the Internal OWD and External OWD of custom objects in Apex? Is there any API or object from which it can be retrieved?

1

These settings can be retrieved using the Metadata API. You could use the Force.com Migration tool or you could write something your own. The settings are in the XML of each standard or custom object retrieved.

UPDATE

For standard object within Apex you have to query the Organization object.

Organization org = [Select Id, DefaultAccountAccess, DefaultContactAccess, DefaultLeadAccess, DefaultOpportunityAccess from Organization];

For custom objects you need to use describe calls and call the getGlobalDescribe()

Map<String, Schema.SObjectType> sharing = Schema.getGlobalDescribe();
if(sharing.containsKey('MyCustomObject__Share')) {
    //custom object is private or public read only
} 
else {
    //custom object is public read write
}
  • It seems Metadata API can't be accessed in Apex. Can you tell something about it? How do I use the Metadata API in my Apex code? – ajinkyah Feb 4 '15 at 10:30
  • Sorry, for some reason I thought you want to access it externally. I've updated my answer. Both of them are valid ways, Metadata API or Apex to achieve what you desire. – Mihai Neagoe Feb 4 '15 at 10:40
  • There are two types of OWD - internal and external which can only be retrieved using Metadata API. And I want to determine these settings in Apex (both of them). – ajinkyah Feb 4 '15 at 10:44
  • They can be retrieved with the Metadata API, but you want them in your Apex code, you just have to query for them and dont't need the Metadata API, the updated answer might help you. – Mihai Neagoe Feb 4 '15 at 10:46
  • 1
    This will give me only the most restrictive setting amongst internal access and external access. How do I distinguish whether the setting is from internal OWD or from external OWD? – ajinkyah Feb 4 '15 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.