Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Is it possible to know, via apex code, if a certain object is Private, Public Read-Only or Public Read/Write?

share|improve this question

3 Answers

up vote 2 down vote accepted

For the Standard object, you can use Organization object to achieve this, query like,

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

System.debug(org);

Query this object to obtain information about an organization's settings. Only one organization object exists per organization.

for the details, see the doc, http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_organization.htm

share|improve this answer
Thank you. That's what i'm looking for. I didn't know that object had these fields. – Ivo Rocha Jan 22 at 12:18

Guess this should help you :

SELECT Id,ParentId,UserOrGroupId,RowCause,AccessLevel FROM ObjectApiName__Share

share|improve this answer
What i want to know is if an certan object is Private, Public or Public RO. If the Sharing table exists, like CustomObject__Share, then I know that this object is private ou public read only, if not it is public read/write. But with standard object I can't use this. For example if I change the object Account to Public Read/Write the object AccountShare keep existing. – Ivo Rocha Jan 17 at 22:46

Another way to test for sharing of custom objects is to check the global describe Map.

if(Schema.getGlobalDescribe().containsKey('MyCustomObject__Share')) {
    //Share object exists, therefore custom object is private or public read only
} else {
    //Share does not exist custom object is public read write
}

As you have already discovered, this is only for custom objects, @JiaHu's answer explains how to get the sharing level of a standard object.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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