Environment Variable – Power Platform

Environment variable enable us to package the configuration along with customization in a single solution. This help us in eliminating the custom entity we use to create in Dynamics which were environment dependent like Url/ email address/ Integration points etc.

Lets do step-by-step process to create and migrate configuration.

We will start by creating one solution and adding Environment Variable to it.

We will create two EV

  1. CurrentUrl
  2. SenderEmail

Both these variable are usually environment dependent. While the URL denotes the D365 instance, the email can be sender email address for which mailbox is configured. We can keep the type as Number/Text/JSON/Data Source. We will stick with Text type only.

The CurrentUrl variable is neither having Default Value nor Current Value.

The SenderEmail variable is having current value but no default value.

These two are important field which we need to pay attention to. Once created, we will export and import the solution.

We are importing managed solution – unmanaged is straight forward.

You will see a Flyout option asking for value of CurrentUrl. If you remember, we didn’t provide value for this in source environment and that is why we need to provide value before importing the solution. But if you have already given the current or default value in source then the flyout menu will NOT appear.

Once imported, if you open the solution then it will not be editable. This is a product behavior (bug?) where it doesn’t allow you to update the config value in the managed solution. If it was unmanaged then it would have been editable.

Now to edit the value, you need to modify the default solution. This can be done by going directly to the table menu or going to default solution or even in advanced find.

Other option is to modify the solution zip file – but lets skip that.

Do remember that environment variable is nothing but two table(entities) in Dataverse (Dynamics 365). The two tables are :

  1. Environment Variable Definition
  2. Environment Variable Value

These two table are 1:N with plugin enforcing 1:1 relationship.

So if you try to add more than one Environment Variable Value for a Definition, you will get an error.

In Power Automate, you will see the Environment Variable under the section as shown below.

In Canvas App, if you are using it as Data Source then you can go to Setting and enable the checkbox.

Otherwise, You can read the Environment Variable just like any other table.

Then you can read the variable just like any row in Dataverse

Set(myVariable, LookUp(
    'Environment Variable Values',
    'Environment Variable Definition'.'Schema Name' = "test_CurrentUrl",

Power Automate : Create Lead from Email with Attachment

Below are steps to create a ‘lead with attachment’ from an ‘incoming email with atachment’.

Lets trigger the power automate flow when the email is created in CRM with direction as incoming. We dont want outgoing email to be converted to lead 🙂

Here we can add further filters like excluding a domain, or checking if body contaiin certain keywords etc

To create a lead, we put the below component in ‘If Yes’ block. We will fill in the username of the email address as the lastname. This might not be preferred choice, but it can be configured as per requirements.

We are using following expression to achive this:

substring(triggerOutputs()?[‘body/sender’],0, indexOf(triggerOutputs()?[‘body/sender’],’@’))

Also, we want to store the email text to description field. Now since the description might have HTML tag which we dont want in our description field – we are using a HTML to text conversion service.

Next we want to find all the attachment linked to this email message. Here we can simply loop through attachment entity.

Please note that you will see two drop down for attachment – one is attachment and other is activitymimeattachment

We need to select activitymimeattachment. So select attachment and then right click to peek code to be sure you selected the correct one,

We are applying filter to get only the attachment of email message as shown :

Remember to use “_activityid_value” and not just “activitiyid” else you will get error message :

“Could not find a property named ‘activityid’ on type ‘Microsoft.Dynamics.CRM.activitymimeattachment’.”

Now we can loop through all the attachment and create annotation(notes) for all the files.

Please note not to fill in the “object type” with entity type when you are specifying the type in regardingobject directly else you might get following error.

The entity with a name = ‘4’ with namemapping = ‘Logical’ was not found in the MetadataCache. MetadataCacheDetails: ProviderType=Dynamic, StandardCache=True, IsLoadedInStagedContext = False, Timestamp=7862004, MinActiveRowVersion=7862004, MetadataInstanceId=21976159, LastUpdated=2021-02-06 09:25:40.947

Also the format of regardingobjectid should be /leads/GUID

This will create lead with following values :

Power Automate | Data Operation | Compose vs Variable

Lets discuss ‘compose’ component under Data Operation in Power Automate. The official doc describe it as “Use the Data Operation – Compose action to save yourself from having to enter the same data multiple times as you’re designing a cloud flow. 

So basically you can store the input here something like this:

The main question that arises is why to use Compose when we have “Initialize Variable” component allready there. Here is how Initilize variable work:

The main differencei s ‘Performance’. So while variable can be setup multiple times, the Compose is more like a constant which can only be initialized once. This give a performace boost when using Compose compared to variable.

Just a run over a big array and doing same operation shows the differnece in the performance.

So if you are intializing a value just once and dont wish to modify it later – use compose. Use variable only when data modification is needed.

%d bloggers like this: