Skip to content

benjaminjohn.de Posts

2 Easy ways to get Environment URL in Power Automate

Posted in Uncategorized

After five years, it’s time to update my post “Get CRM URL in Power Automate“…

Cases where you might need to get the Environment URL

  • When you ‘Relate’ or ‘Unrelate’ two records with the Dataverse action
  • For generating a direct link to a specific record for activities or other records
  • If your Flow logic has different branches for the production environment then for the others. For example in approvals processes, for email send outs or or integrations with third-party systems.

Options for retrieving Environment URLs in your Power Automate Flows

1. Environment Variable

Environment Variables are configurable values used within solutions. They follow the KeyValue principle, where the Key is fix defined and the Value can vary between the different environments. The value gets set during the deployment of the containing solution.

Here are two links where you can find informations about Environment Variables and how to use them in Power Automate

2. Previously used Dataverse Action

If you already use a Dataverse Action (not a Dataverse Trigger) in your Power Automate Flow, it might be worth checking out it’s output. Maybe you find there the Environment URL.

Environment URL

It doesn’t play a role for us if the ULR ends behind the domain (.com), or if it has paths (.com/path/subpath) and / or parameters (.com?ad=YourAdPlacement) behind it.
The following expression ignorantes all elements behind the domain and returns a clean URL.

join(take(split(outputs('YOUR_AD_PLACEMENT'), '/'), 3), '/')
Do not forget to replace my example content “outputs(‘YOUR_AD_PLACEMENT’)” with your content.

My preference

As most of my Power Automate Flows operate in Dataverse environment, there is mostly already a Dataverse action where I can pick the URL from. There is also no chance for a human error during setup, compared to Environment Variables.

KeyBox – A Dataverse solution to govern credentials

Posted in Power Platform, and Dynamics 365

Most of the time, I create solutions based on customer requirements. Sometimes, I create solutions to learn something new and “KeyBox” was my last personal project to learn some new skills.

KeyBox – The features

It is a solution to store and govern credentials (stand alone or in regard to a customer) in Dataverse. You can store usernames & passwords or client IDs & client secrets. It reminds you when the “valid until” or “remind me on” date is reached. Additionally, a note can be placed at the MFA switch and a URL.

KeyBox Record

Additionally, you can create new passwords, based on the rules of the organization.

KeyBox Config

I’ve learned in my KeyBox project:

Get your KeyBox!

Sounds like I try to sell you something, but I don’t. I put it on GitHub under MIT licence. Means you can use and modify it in any way you want.

KeyBox

Retrieve EnvironmentVariableValue in JavaScript

Posted in Power Platform, and Dynamics 365

In my private project (Key Box), I stored a JSON configuration in an Environment Variable. To retrieve its value, I had could use two queries. The first query to retrieve the GUID of the Environment Variable Definition and the second query to retrieve the Environment Variable Value. But there is a better way: the RetrieveEnvironmentVariableValue WebApi function.

Below is the async function I wrapped around it to make it easy reusable.

Retrieve EnvironmentVariableValue JavaScript

async function getEnvironmentVariableValue (schemaName) {
  return Xrm.WebApi.online
    .execute({
      DefinitionSchemaName: schemaName,
      getMetadata: function () {
        return {
          boundParameter: null,
          parameterTypes: { 
            DefinitionSchemaName: { 
              typeName: "Edm.String", 
              structuralProperty: 1, 
            } ,
          },
          operationType: 1,
          operationName: "RetrieveEnvironmentVariableValue",
        };
      },
    })
    .then(function success(response) {
      if (response.ok) {
        return response.json();
      }
    })
    .then(function (responseBody) {
      return responseBody["Value"];
    })
    .catch(function (error) {
      console.error(error.message);
    });
}

Retrieve EnvironmentVariableValue in Power Fx

Posted in Power Platform, and Dynamics 365

In my private project (Key Box), I stored a JSON configuration in an Environment Variable. To retrieve its value, I use two queries. The first query to retrieve the GUID of the Environment Variable Definition and the second query to retrieve the Environment Variable Value. I don’t use the Retrieve EnvironmentVariableValue function because I also need to update the environment variable later.

Power Fx to query an EnvironmentVariable and its EnvironmentVariableValue

1. Get the environment variable definition record by schema name

Set(
    envVarDefRec; 
    First(
        Filter(
            'Environment Variable Definitions';
            'Schema Name' = "new_YourSchemaName"
        )
    )
);;

Set(name; value) to create the variable envVarDefRec and store the value.
First(table) to get the first record of the passed table.
Filter(table; condition) to filter the Environment Variable Definitions table by scheme name.

2. Get the EnvironmentVariableValue record

Set(
    envVarValRec; 
    First(
        envVarDefRec.'Environment Variable Values'
    )
);;

Set(name; value) to create the variable envVarValRec and store the value.
First(table) to get the first record of the passed table.

3. Get the value from the EnvironmentVariableValue record

Set(name; value) to create the variable envVarVal and store the value of envVarValRec.Value.

Set(envVarVal; envVarValRec.Value);;

Bonus: Apply the default value from EnvironmentVariableDefinition record if the EnvironmentVariableValue is empty.

If(condition; then) to do the next action only if the condition is fulfilled.
IsBlank(value) to check is envVarVal contains data.
Set(name; value) to fill the variable envVarVal and store the value of envVarValRec.Value.

If(
    IsBlank(envVarVal);
    Set(envVarVal;envVarDefRec.'Default Value')
);;

Update EnvironmentVariableValue in Power Fx

Posted in Power Platform, and Dynamics 365

In my private project (Key Box), I stored a JSON configuration in an Environment Variable. The configuration is displayed in a custom page and can be edited there. Here is how to update an EnvironmentVariableValue in Power Fx.

To update a Dataverse record, you need a reference to it. In my case, I already retrieved it as record in the step before, stored as global variable “envVarValRec” and I just reuse it now.

Update EnvironmentVariableValue in Power Fx

Patch(
   'Environment Variable Values'; // the table name
   envVarValRec; // the record to update
   Defaults('Environment Variable Values');
   {
      Value: "payload" // the string to store
   }
)