Logic Apps: How to add several actions inside a Logic App condition branch

Posted: April 20, 2016 in Azure App Services, Logic Apps
Tags: , , , , , , ,

In my last blog posts 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 a 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 provide 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 provide 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 Smile)

Creating a Logic App with conditions inside

Let’s create a Logic App call “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

01-Logic-Apps-Conditions-Add-Tweet-Appear

  • Select the plus sign, and then choose “Add a condition

02-Logic-Apps-Conditions-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:

03-Logic-Apps-Conditions-note

do not be alarmed, there is nothing wrong in 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 for 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:

04-Logic-Apps-Conditions-side-by-side

  • Now that we have the condition action we need to configure in 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

05-Logic-Apps-Conditions-Tweet-condition-configuration

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)

06-Logic-Apps-Conditions-add-action-if-branch

  • 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:
   
{ }

07-Logic-Apps-Conditions-add-action-if-First-action

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.

08-Logic-Apps-Conditions-add-action-if-second-action

  • 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
  • On the “If no, do nothing” branch, click “Add an action
    • From the search box select “Show Microsoft managed APIs” type “email” and select the “Office 365 Outlook – Send Email” action
      • On the “Subject” property, statically set as “Tweet related to Logic Apps
      • On the “Body” property set the parameter “Tweet text” from the output of “When a new tweet appears” trigger
      • On the “To” property, set the email of your manager Smile

09-Logic-Apps-Conditions-add-action-if-no-email-action

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

13-Full-Logic-App-conditions

Save” your logic app and wait to a tweet to appear, we will see that a new file was added in 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 file name
  • And create a file in dropbox

We instead of having several action, creating and repeating the same steps over an over again, we can call the “AddFileToDropbox” to do the same steps for us.

10-Logic-Apps-Conditions-add-action-if-call-Logic-App

Advantages of this approach:

  • We are reusing “code”
  • We can add “nested” conditions

Disadvantages:

  • ·We loose 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.

11-Logic-Apps-Conditions-runs-action-history

12-Logic-Apps-Conditions-runs-action-history-2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s