- 11 Nov 2022
- 2 Minutes to read
Create and Call Nested Logic Apps
- Updated on 11 Nov 2022
- 2 Minutes to read
When writing code, developers want a way to modularize their code so that they can reuse it and call it from other classes and methods.
The need to call reusable logic also exists within Logic Apps.
Consider a scenario where you encounter an error within your logic app processing and you want to create an Incident Ticket within an IT Service Management (ITSM) tool like ServiceNow. You wouldn’t want to repeat this same ServiceNow logic in every logic app that you build. Instead, you can centralize this logic to connect to ServiceNow and simply call it from your other Logic Apps.
In this post we are going to build a nested logic app and then demonstrate how we can call it from other logic apps.
Nested Logic App
Let’s first build out our nested (or shared) logic app. In this case, we will add an HTTP Request trigger and define some message body attributes including Logic App Name, Error Message and Category. We will use these values to populate a ServiceNow Incident Requests. We will then return the ServiceNow Incident Number to the calling logic app.
Parent Logic App
We will now build out a logic app that will call our nested, or child, logic app. To keep things simple, we will add a Recurrence trigger and then add an HTTP action. This HTTP action we will get to fail, on purpose, by including a bad URL. Next, we will add the Call Azure Logic App action and choose the selected logic app that we want to call.
Since we included message parameters in our nested logic app HTTP trigger, we see parameters that we can pass. For the LogicAppName parameter, we will use an expression of: workflow().name which will pass along the display name of our logic app. The Workflow and Trigger name fields will be auto populated.
Now, we only want to call our nested logic app when our HTTP action fails. As a result, we will Configure run after settings an ensure that has failed is selected.
If we run our (parent) logic app, we will discover that our HTTP action fails, as expected and our nested logic app is called.
We know that our nested logic app succeeded as it returned a ServiceNow TicketNumber.
In this post, we discussed how we can create nested logic apps that can be called from other logic apps.
This allows us to centralize common logic.
In the event we need to change this logic, we can change it in one place instead of many places if we would have embedded it in other logic apps.