5

Is it possible to control the Organization Wide Defaults or Default Sharing Settings in effect when we run unit tests?

Ideally we should be able to set different OWDs as a step in test setup so we can ensure the code will behave correctly under different OWDs that could be set in different organizations where the package/code is installed?

How do I know what OWDs are set by default for a test run session?

EDIT: I've noticed that if I set OWDs for my DEV org, then that's what applied for the unit-test session, which makes sense. But the issue is how do I change it as part of unit tests.

My application behavior changes the OWD setting for Contact object. If Contact OWD is 'Public Read/Write' it does one thing, and does another if it's 'Public Read Only' or less.

I'd like to test this change with a unit test, that it behaves correctly under different configuration.

Does anyone know how to do either of following:

  1. Set the OWDs applicable for the unittest session using Apex in a test setup method? (Original Question)
  2. Run the same set of unittests targeting different DEV organizations on which I set different OWD configurations manually? (Not the ideal, but something is better than nothing).

1 Answer 1

3

I don't know about the first part of your question on how to change the OWD's. You can use the Organization object though to find out the default OWD's for the org you are in.

This allows you to query for the default access for standard objects

Organization myOrg = [Select Id, DefaultAccountAccess, DefaultContactAccess from Organization];    
System.debug(myOrg);

Unfortunately, the fields are not updateable so you can't change the OWD in this manner, only query for them.

Here is the documentation on the Organization object. note that it states 'Only one organization object exists per organization.'

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_organization.htm

3
  • Thank you very much for the information, it does answer the second part of my question.
    – BuddhiP
    Sep 25, 2013 at 17:36
  • No prob, glad to help. I will be interested to see if someone has a solution to the first part of the question. Sep 25, 2013 at 17:53
  • This does not give information about any Custom Object in the org. Do you know how can we read if certain Custom Object is set to Private or Public Read Only Sharing Setting in APEX code?
    – VarunC
    Jul 24, 2019 at 17:26

You must log in to answer this question.

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