In my last blog post, I talked about the new “The ability to call nested Logic Apps directly from Logic Apps Designer” and I mentioned that this great feature, not only allow us to create reusable pieces, but that will allow us to overcome some of the current Logic Apps limitations:
- The ability to add more than one action inside the condition branch’s (this is actually a false statement);
- or the ability to add more than one action inside the loop;
In this post we will address the first option, we will leave the second one for a dedicated post. Once again, in my previous post, we created a Logic App called “AddFileToDropbox” that:
- Its trigger manually, which means through an HTTP post, allowing to be invoked by other Logic Apps
- Then it will call an Azure Function to dynamically generate a filename;
- Create a Dropbox file
- The filename it’s provided by the Azure Function;
- And the content of the message it’s passed in the HTTP request that triggers this Logic App.
- And finally, return an HTTP response based on the status of the file creation
We will reuse this “child” Logic App in our demos here.
Our “basic” demo scenario
The important thing here is to demonstrate the concept itself, not whether this is a real case or not, so, we will use connectors that are simple to test.
Let’s say that we want to listen to #LogicApps twitter hashtag, and:
- If the tweet was made by someone other than me (@sandro_asp) then:
- We need to call an Azure Function to dynamically generate a filename;
- Create a Dropbox file
- The filename it’s provided by the Azure Function;
- And the content of the message will be the actual tweet text message.
- If it’s a tweet made by me, let’s say I want to send it in an email to… my manager (just to annoy him)
Creating a Logic App with conditions inside
Let’s create a Logic App calling “ConditionsInsideLogicApp” (you can see the basic steps in my previous posts)
- Add a “Twitter – When a new tweet appears” trigger, to look for a new tweet containing the hashtag “#LogicApps”
- Select the plus sign, and then choose “Add a condition”
Note: if by any chance, you verify that the conditions branches, “if yes” and “if no, do nothing“, are not side-by-side as would be expected, but instead they are in sequence, one under the other:
do not be alarmed, there is nothing wrong with your editor. It just means that your monitor does not have enough resolution, you are using a large zoom or your browser window is too small to render the conditions side by side. If you switch for a different monitor or zoom out, you will see that they will be rendered side-by-side:
- Now that we have the condition action we need to configure in the following manner:
- In the “Object name” property we need to set the parameter “Tweet by” from the output of “When a new tweet appears” trigger
- In the “Relationship” property we need to choose the option “is not equal to”
- And finally, on the “Value” property, we need to statically set the value “@sandro_asp”
Note: If we look to our “If yes” branch we will see that we only have the ability to “Add an action” with means that we cannot create, at least for now, nested conditions (you probably can if you call another Logic App)
- If we go to our “If yes” branch and “Add an action”
- From the search box select “Show Azure Functions in my subscription” and select the Azure Function: “CreateFileName” and set the “Input payload object” as an empty JSON message:
{ }
Based on your user experience in the Logic App designer, you may think that you cannot add more actions inside the “If yes” branch because you don’t have the plus sign underneath the Call Azure Function action. Well, you are wrong!
You actually can add several actions (conditions are not allowed) inside the same branch, by clicking once again the “Add an action” on the top right corner.
- From the search box, type “Dropbox” and select the “Dropbox – Create file” action
- Set the Folder path, let’s say the root by typing: /
- Click on the Filename and set the parameter “Body” from the output of “CreateFileName” function as input
- Switch to code view to edit Create file inputs to have the expected values
@{body('CreateFileName')['FileName']}
- And then click on “File content” text box, and set the parameter “Text” from the output of “When an HTTP request is received” as input
At the end your Logic App will have a condition with:
- Two actions in the “If yes” branch
- And one action in the “if no” branch
“Save” your logic app and wait to a tweet to appear, we will see that a new file was added to your Dropbox or an email as sent to your manager
Second option
Because we already created a Logic App that makes the necessary steps for the condition “If yes“:
- Generate a filename
- And create a file in dropbox
We instead of having several actions, creating and repeating the same steps over and over again, we can call the “AddFileToDropbox” to do the same steps for us.
Advantages of this approach:
- We are reusing “code”
- We can add “nested” conditions
Disadvantages:
- We lose a little the run action history because we then have the action “AddFileToDropbox” but we cannot easily go to the child run and correlate the existing runs.
Explore more on: Tracking Message Properties