Question: Is there any option to automatically kick off an X++ job (or any automated task) after a sandbox refresh?
Info on Current Sandbox Refresh
This article explains how refreshes work for cloud hosted (sandbox) environments: https://docs.microsoft.com/en-us/dynamics365/unified-operations/dev-itpro/database/database-refresh
It points out that the refresh will:
- Disable all users other than admin
- Disable all batch jobs
Another side effect is where you use accounts specific to an environment those accounts are overwritten by the refresh, so need to be recreated. E.g. we connect to our environments via OData, and use different accounts for accessing production and test to help silo the environments / reduce the risk of accidentally connecting a test system to production or vice versa, and thus creating an issue; so after a refresh we have to replace `interfaceAccountProd@example.com` with `interfaceAccountNonProd@example.com`.
Info on Automation
It looks like Raphael Taglilani's done some work towards automating post-refresh tasks: blog.zealots.solar
He provides SQL and PowerShell scripts for this. Most of the focus is on refresh non-sandbox environments from the refreshed sandbox; but there are some other post-refresh bits (e.g. enabling all users).
Sadly running such scripts still needs to be run manually.
What would be excellent is if there were some event driven process such that once a refresh completes successfully a job can be fired off to perform a series of tasks. For most use-cases I'd assume this would include:
- Enabling all users (or a specific subset, say all users with the `system administrator` role / all users listed in a resource file)
- Putting all withheld batch jobs back to waiting status
- Amending any external email addresses to dummy ones (e.g. so tests don't accidentally fire off invoices to external customers) / any other data masking activities.
However the key point is how you detect that the refresh has occurred / completed before triggering the scripts to correct the data.
Thoughts
A hacky approach which may work would be to have a scheduled task run on the VM every 5 minutes and run a query which signals this change (i.e. for my use case we could look at the interface account ; when we see the production account in a test environment that triggers our other scripts to connect to the database and update the data). A more generic approach may be to check for a change of GlobalGuid; though it looks like that may no longer be used (i.e. https://tenant.sandbox.operations.dynamics.com/?cmp=DAT&mi=SysTableBrowser&TableName=SYSSQMSETTINGS shows this field still exists, but returns no records).
However, this frequent polling approach has a bad smell to it. Is there a better way?
Thanks in advance for all thoughts & feedback.