Give me a fresh sandbox copy of my D365 BC production environment each night


Me: "What features do you really not want to lose from your current system"

Them: "We really like the copy of production that gets generated each night. We can test & train in a system that's up-to-date to the day before with everything"

This was part of a conversation with a company looking to migrate from NAV on-prem. Good old SQL Server Agent you could have that do so much! Thankfully, we have Power Automate to pickup these kind of things now.

Here is a PA flow using BC admin APIs to meet the requirement of a creating a fresh Sandbox copy of Production each night.  

Previously I had played around with one of the BC admin API's to get capacity stats BC database capacity Power BI Report. Shout out to AJ Zafar for the help he gave me on the initial OAuth getting the token and the Try/Catch scopes approach! 

Here I'm using the HTTP DELETE method to remove a sandbox, to free up space and then an HTTP POST copy method to trigger the fresh copy of production. In this example I've hard coded the name of the environment that will be replaced each night!


Here are the details of the actions

[1] the tenancy GUID. [2] Client ID of the OAuth App

The admin API is authenticated with an OAuth 2.0 token - check out this post if you want details on setting that part up Dynamics 365 Business Central - OAuth 2.0 APIs with Insomnia.

Parse to get the access token out of the OAuth token request response

Down to business now, lets delete a sandbox to make space for a fresh copy. Business Central Admin Center API - Environments - Business Central | Microsoft Learn

[1] The name of the environment to be deleted. [2] the OAuth token

I put a 5 mins pause in between this action and the next, for larger environments maybe this needs to be longer. Or maybe not, since no data is deleted at this point it might always just take a few mins. 

Timings of the delete action (soft delete in the operations pane)

To create a copy of Production here are the details. Business Central Admin Center API - Environments - Business Central | Microsoft Learn  



[1] The environment to be copied. [2] Name of the copy. [3] Environment type of the copy

Finally the error Catch scope - set to run if the Try scope has any errors


That's it. Have you used the BC Admin APIs to help automate an administrative tasks?

What do you think?

Please let me know your thoughts in the comments

Connect or follow me on LinkedIn to get all my updates Andrew Wingate | LinkedIn

Click to Follow