Have you ever had to change different values in your flow when adding them to different environments? For instance, different queue IDs as an email sender, different email template IDs, receiving and routing emails to different queues, or any sort of ids specific for each environment.
Power Platform is an own action that allows you to get the environment your flow runs and by using this you can create variables and set the correct values for each environment and use the variables in the places you otherwise had to change manually.
This post shows you how you can check for the environment in your Power Automate flow runs. The post shows only an example of how to check for different environments and how to set and update variables based on environments. In the examples, the compose action is used to only show the principle of how it works.
Follow the steps below to see how you can check the environment your flow is running in Microsoft Dataverse.
Step 1: Create a new flow with a Dataverse trigger. In this example, the trigger “When a row is added, modified or deleted”. This triggers when a case is created and updated. During the examples below a case was created one time and later used the test recently triggered runs.
Step 2: Add an action called “Get Environment as Admins”, this is listed under the “action collection” Power Platform for Admins. This doesn’t show up easily by scrolling and searching parts of the name. To make it show you need to search for almost the whole name. This might change after the time of writing.
Step 3: After opening the Power Platform for Admins collection search for “Get Environment as Admin” or scroll in the list until you find it.
Step 4. When the action is added we need to select the environment we want to get. In the dropdown, we get two environments and a choice to enter a custom value. To find the current environment dynamically we need to add an expression that gets the name of the environment that the Power Automate flow runs. This action (the one below) then uses this name and lists the current environment. To add the expression proceed to the next step below the image below.
Step 4: Add the expression to get the current environment in which the flow is running. The following expression is added to the “Get Environment as Admin” action as shown in the image below. To add the expression make sure you chose the “Enter custom value” in the previous step.
Step 5: After the “Get environment as Admin” action we can use a compose action to output the display name of the environment. I like to you the compose action to output values that are going to be used later in the flow. To output the property we are going to use for checking the current environment we can add the Display Name from the previous action shown in the image below.
Step 6: To see the Display Name of the environment the flow is running we can run the flow and see the name in the compose output shown in the image below.
Step 7: Next we need to check if the environment is the one that we want it to be.
If you have two environments you can use a regular condition, and if there are more than two environments we can use a Switch/Case condition. The image below shows a regular condition and a switch condition. The next step we take a look at each of them.
Step 8: Checking for an environment with a regular condition. Yes/No condition in Power Automate.
Assuming you only have to environments, test, and production. You can use a regular condition as long as the flow only runs in these two environments. By using the Display Name of the environment and checking against the name we got from the compose output we can check for the product name. If Display Name and the “hard coded” production name matches the flow runs in the production environment, otherwise it runs in the test environment.
Step 9: Checking for more than two environments using Switch.
If there are more than two environments, let’s say development, test, and production. We can use a Switch/Case condition. By using the same principle as the regular condition we can get the name of the environment by running the flow in the desired environment and get the Display Name that way or by going to the Power Platform Admin Center, https://admin.powerplatform.microsoft.com/.
By using the admin center to get the Display Name it might not be the correct name. As you can see in the example in this post, the name of the environment is “Playground” but the Display Name we check against is “Playground (org041db96e)”. Be aware of this if you see that the condition doesn’t go to the environment you want it to.
Notice: In the example below the name of the environments in the Switch/Case below are not existing environments in this Trial instance, this is to show how you can check for different environments using the switch condition.
Hope you found it valuable!