We're performing automated testing on scratch orgs and we want to change Organization-Wide Default Sharing for some objects.

Doing so via the UI or via sfdx force:mdapi:deploy causes sharing rules recalculation, which is to be expected.

Warning One or more sharing operations has been initiated. See below for additional details. Certain operations may not be available.

We want to wait until all recalculation is complete. Previously we just queried the changed object's metadata (Edit: Using the Metadata field on the EntityDefinition object via the Tooling API) to check to see if the rules had been applied. However, when changing the Account object's sharing model, it seems like the metadata changes before the whole recalculation operation is complete (probably due to some related objects being recalculated).

We tried querying ParallelJobStatus (which doesn't work), CronTrigger, AsyncApexJob and BackgroundOperation (which we think are not the right objects for this, and which return nothing anyway).

Is there a way through an API to be 100% sure that sharing recalculations are completed?

Thank you.


We're testing sharing rules and models automatically, so we need to be sure sharing calculations are applied before moving on. ExternalSharingModel, InternalSharingModel doesn't always play well with ControlledByParent objects.

Example: A ControlledByParent Contact or Order will not appear as ControlledByParent, they will be the same as Account. We sometimes want to check if an object is really ControlledByParent.

It seems like changing Account's sharing model creates other background jobs to update children (Contact, Cases and such) that are controlled by parent. This means that Account has the right Metadata, but sharing rules are still calculating for other objects. We can't really know when these are done, since they are still Controlled By Parent (they haven't changed).

  • 1
    When you say you queried, were you polling the describe data, pulling via Metadata API, using the Tooling API on EntityDefinition, or something else?
    – David Reed
    Feb 28, 2020 at 17:34
  • We're using the Metadata field on EntityDefinition via the Tooling API. Feb 28, 2020 at 18:03
  • I query it directly (SELECT ExternalSharingModel, InternalSharingModel FROM EntityDefinition) and it seems to work, but I'm doing this in scratch orgs that contain little to no data. What are the symptoms of this approach not working? Does the next sharing operation fail?
    – David Reed
    Feb 28, 2020 at 18:49
  • I edited the question with a little more clarification. But yes, the next sharing operation does fail if not done at the right time. Mar 1, 2020 at 0:43
  • If you can maintain the state of OWD for all objects and compare them before and after deploy, will that not make sure that the calculations have completed. Even in case of updates to the child objects, eventually, the updates will complete and the OWD on child object will change. What you need to maintain is how the updates to Account OWD or any other object impact the OWD of child objects and if you check after some time via polling that all the OWD of child object have changed in accordance to list you are maintaining, we can conclude that the update is finished.
    – manjit5190
    Mar 5, 2020 at 13:51


Your Answer

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