Today the Swiss Army knife in Power Automate. The Compose Action is extremely useful when you have to perform any operation on your data, store some value or use it for debugging.
We’ll go over the cases in a bit, but the Compose Action enables you to build expressions that generate any object.
Where to find it?
You can find it under “Built-In”:
And then select “Data Operations.”
Here’s how it looks like:
It could not be simpler. You provide input, and it returns an output. We can do a lot, so let’s break them into sections, but I’m sure you can find more usages for it.
You can go crazy with expressions inside a Compose Action. Since it enables input, you can do the data processing inside the compose and further in your Flow. For example, let’s say that you get an URL and convert it from a relative to an absolute URL. You can use the following formula:
replace(<string with the relative url>,'href=''/','https://manueltgomes.com')
The Compose Action will return a string with the correct URL that you can use in your Flows.
Let’s imagine that you have an input and you want to check if it will fit in the destination field. We can use a compose field to get the field’s length and then use it after validation.
In this case, we’re using a length function to get the number of provided characters.
You may ask, why not use the “Condition Action” to do this? You can, but it’s useful to have the field pre-calculated so that you can use it in multiple places. Also, it’s faster than a variable because you can quickly write the function and generate the object.
Debugging Flows hard, but the Compose Action helps a lot on this. I advise using it to do data processing because in the “Run history,” you can see what the input was and what came out before using the value. With this, you can check if there are issues in your expressions and fix them. If you’re using it inside your “Condition Action,” you’ll only get a “true” or “false,” and that’s not very useful to understand what happened if something went wrong.
Please note that you can put any value, so you can even use it to “log” a value before it’s transformed.
In this case, we’re using the value that the user-provided. You may say that you can check the initial value, and you’re right, but, depending on how complex the Flow is, having the value right before it’s parsed goes a long way to have all the information “at hand” when you’re debugging your Flow.
Why not variables?
I get this question a lot, and some people don’t understand the advantage of the Compose Action when you have variables that do, apparently, the same job. Both serve their purpose and have advantages and disadvantages, but I like to use Compose Actions when
Parse data Inside Actions
I want to generate a value or parse it inside actions. Since variables need to be initialized in the “root” of the folder before you can use them, it’s cumbersome to have one action to initialize the variable and another inside your actions to use the variable. The Compose Action enables you, in one step, to have an input, perform the actions and return the output, regardless of the nesting level.
Variables are good to store data and do some actions like increment integer values, but a Compose Action is much more useful for parsing data. You don’t need to define the type of object, and you can use it on the spot where you need it.
Another issue/advantage with variables is that you may use them anywhere in your Flow, and they keep the values. If you make a mistake and use the wrong variable, you may be injecting the wrong data in the Flow step. In the Compose case, since you create it “on the spot,” you’re always sure that you’re using the value generated before.
Break down complex actions
Another great usage for the Compose Action is when you have a very complex expression and want to break it down into pieces. Having to declare variables for each of the steps can be cumbersome, but you can have sequential Compose Actions that parse the previous value so that you can:
- See each step and if the data is being parsed as you want it to be.
- Break down things into simpler expressions, making debugging easier.
- Change only a small part of the “chain,” and all the others remain intact.
There aren’t many limitations to the Compose Action, but its flexibility can be a problem if not used properly. While using variables, you define the type of object you want from the start (array, string, etc.). But since the Compose Action enables you to have any input and any output, you may be generating outputs that break your Flow.
Here are some things to keep in mind.
Name it correctly
Like I mentioned before, Compose Actions are incredibly flexible, so having a name that indicates what it’s doing is super important. Your name should indicate what you’re doing and, if it’s a series of steps, indicate the number of the step with the description.
Always add a comment.
Always indicate in the action what its intention. It’s important to fill it before adding the parameters to know what you want to do. After a while, you can then check if the comment matches what’s really happening when you’re debugging something.
Back to the Power Automate Action Reference.