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?
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).