Adventures with Logic Apps: Working with @Outputs

In a previous article, Logic Apps Calling Logic Apps, I show how to have a parent Logic App call child Logic Apps.

Now I want to have my child Logic App return a value that my parent Logic App can do something with.

This article assumes you are familiar with Logic Apps.

Getting started

We will create two Logic Apps.

The first Logic App will accept a property for State, e.g. MI or OH, and return a Sales Tax Rate.

The second Logic App will accept a property for State and Amount, then it will call the first Logic App to get the Sales Tax Rate and finally return a the Amount, Sales Tax Rate, Sales Tax Amount and Total Amount.

Sales Tax Rate Logic App

Let’s create the first Logic App as an When a HTTP request is received trigger.

This Logic App will accept a state property in the body and return the sales tax rate for that state, if the state passed is not valid a status code 400 - Bad Request will be returned.

Set the Request Body JSON Schema to the following:

 {
    "type": "object",
    "properties": {   
        "state": {
            "type": "string"
        }
    }
} 

Next, add a Switch Control action.

Right now we are only going support Michigan, or MI as a valid value for the state parameter.

For the Switch, set On to state.

For the case, set Equals to MI.

Add a Request Response action to the case.

Set the Body to the following:

{
     "sales_tax_rate": 0.0625
 }

For the Default case, add a Request Response action.

Set the Status Code to 400.

Set the Body to concat('The state ''', triggerBody()?['state'], ''' is not valid.').

Now let’s create the second Logic App that will consume this Logic App.

Amount Calculator Logic App

This Logic App will accept an amount and a state property.

If the values provided are valid the Logic App will return a OK status code along with the amount, sales_tax_rate, sales_tax_amount and total_amount.

Create a new Logic App as an When a HTTP request is received trigger.

Set the Request Body JSON Schema to the following:

{
    "type": "object",
    "properties": {
        "amount": {
            "type": "number"
        },
        "state": {
            "type": "string"
        }
    }
}

Add a new Azure Logic Apps action.

Select the Logic App that we created in the first step, and select manual for the action.

Set the value of state to the value of state from the When a HTTP request is received trigger.

Now add a Request Response action.

For now, set Body to the following:

{
	"amount": "0",
	"sales_tax_rate": "0",
	"sales_tax_amount": "0",
	"total_amount": "0"
}

Switch to Code View and scroll to the Response action.

Set the value of amount to @triggerBody()?['amount'].

Set the value of sales_tax_amount to @mul(outputs('SALES_TAX_RATE_LOGICAPP_NAME')['body']['sales_tax_rate'], triggerBody()?['amount']) .

Set the value of sales_tax_rate to @outputs('SALES_TAX_RATE_LOGIC_APP_NAME')['body']['sales_tax_rate'].

Set the value of total_amount to @add(triggerBody()?['amount'], mul(outputs('SALES_TAX_RATE_LOGIC_APP_NAME')['body']['sales_tax_rate'], triggerBody()?['amount'])) .

Replace SALES_TAX_RATE_LOGIC_APP_NAME with the name of the first Logic App we created, also All make sure to enclose all values in double quotes, ".

Wrapping it up

You can now test the Logic App from Postman.

Just a heads up, if I was doing this for a production environment, I would leverage the Variable actions Variable initialize and Variable set to hold the values, doing this would make the Logic App more maintainable and readable.

Thanks for reading!

Leave a Reply

Your email address will not be published.