Is there a smart way to get an Environment Variable in a flow?
Jonas “Giving XrmToolBox a good” Rapp
Say no mo…
If you want to get all fancy with ALM and environment variables, read Colin’s readers digest. For simpletons like me, all I want is to get the current value of a variable in a given environment. You know, like email address for notifications, this kind of stuff.
Prep work
- First, we want to create a solution (duh, won’t work otherwise).
- +New > Environment Variable
- Pick the name, the type, default, and current values
- This is what I have selected:
blurred part is deliberate, this is what we’re going to retrieve!
Child flow
We want to create once, use many (remember the “smart way” part of the original question?). Let’s create a flow that can then be invoked from elsewhere.
- Use PowerApps trigger from PowerApps connector. That makes this flow callable from either Power Automate or PowerApps. Nice.
- Add List records action from CDS (current environment) connector.
- Entity Name: Environment Variable Definitions
- Select Query: environmentvariabledefinitionid
- Filter Query: displayname eq ”
(yes, empty string, hold your horses) - Top Count: 1
(entirely optional, I just would like to be very specific)
- Rename step into GetDefinitions
- Position cursor between single quotes in Filter Query, and insert Ask in Power Apps value. You’ll get the name GetDefinitions_SelectQuery inserted. If you didn’t rename the step would have been List_records_SelectQuery and there is no way to get rid of that besides rebuilding the trigger.
- Add another List records action from CDS (current environment) connector.
- Rename the action to GetVariable
- Entity Name: Environment Variable Values
- Select Query: value
- Filter Query: _environmentvariabledefinitionid_value eq ‘<expression>’ where expression is outputs(‘GetDefinitions’)[‘body/value’]?[0]?[‘environmentvariabledefinitionid’]
(basically grabbing the first definition id returned by the previous action)
- Top Count: 1
- Add Respond to a PowerApp or flow action from PowerApps connector
- Type of output: Text
- Name: Value
- Value: expression outputs(‘GetVariable’)?[‘body/value’]?[0][‘value’]
- Name your flow GetEnvironmentVariable, save
Using the child
You know that tipsters are big fans of child labor in automation. This one is no exception
- Create manual flow
- Add an input parameter Variable Name of type text
- Add Run a Child Flow action from Flow connector
- Select GetEnvironmentVariable child flow from the list
- Set GetDefinitions_SelectQuery to Variable Name from previous step
- Add a Compose step to display the output
Testing
- Run the manual flow
- When prompted for parameter, type Ok Boomer
- Check the results of the flow
Yes, the output is the current value of the variable we blurred out at the beginning.
Notes
- Yes, I knew and hard-coded the data type. Technically, you can retrieve the type from the definition and add a switch so that you can return various data types. Though with JSON on offer, not sure I’ll be using any other type any time soon.
- You probably should be using schema name of the variable and not the display name.
- Yes, you can call this flow from the canvas PowerApps as well. That’s the cool part.
- Yes, you’d want to add some rudimentary error handling. For example, if the variable definition is not found.
- Get your copy here:
Cover image from Pexels courtesy of Mike