Wednesday 14 September 2022

Azure Logic App - A Brief Introduction With An Example

 In Azure the Microsoft providing app services. The app services include web apps, logic apps, mobile apps, etc.

 
All these services are coming under PaaS.
 
As per Microsoft's definition, logic app is a cloud service which can be used to schedule, automate and orchestrate tasks, business process and workflows.
 
Logic apps can be used to integrate applications, data, services, systems across various enterprises or organizations.
 
The business process modeled in a software is called Workflows.
 
We can use the Azure logic app to automate a common business process that can be shared by multiple applications or enterprises.
 
For example, we can use the logic app to send email notifications to different users when an event happens in various applications, services, and systems, etc.
 
Similarly, we can use the logic app to transfer/copy a file uploaded to the blob storage to another location based on specific conditions. 
 
Another example might be to create a thumbnail image when a new image uploaded the blob storage.
 
In logic app, we can create a business process graphically using the workflow engine and visual designer and connect them through connecters.
 
Microsoft already developed connectors to interact with different applications. 
 
We have connector to connect to MS SQL, connector for MySQL, connector for Facebook, connector for Twitter, etc.
 
The beauty of the Azure logic app is we can do this all without writing a single line of code  
 
In short, we can say the Azure logic app help us to automate business processes.
 
Let's create an Azure logic app in Azure portal.
 

Create an Azure Logic App

 
In this article, we are going to create a logic app to read Azure database records and send mail to the users by checking the value of the mailSent column value.
 
I have already published an article about Azure database creation and use.
 
You can find the article here >> Create And Use SQL Database In Microsoft Azure
 
I am using the same database and table for our logic app.
 
In our Azure database, we have a table called tbl_problemTickets.
 
This table has 4 columns - ID, prblm_description, usermail, and mailSent.
 
Let's discuss our requirements next. 
 
Just assume we have an application to create client issues as problem tickets. The client can create different problem tickets for support.
 
And, we need to send an email to the client when they create a new ticket like an acknowledgment mail.
 
Once the email sent, we should update the mailSent column as 1 (initially it will be 0) 
 
Let's discuss how we can accomplish this task using Azure logic app. 
 
To create logic app please follow the below steps,
  • Open Azure portal
  • Search for Logic App



    We will be navigated to the below screen.

  • Click either Add button or Create Logic app button to create the logic app



  • A new section will be visible to provide the basic details about our logic app like name, resource group, location, etc.

    Fill the details and click the Create button. Please refer to the screenshot below.



    This will take a few minutes to create the Logic app.

  • Once it has been created, We can see the success notification in the notification section.

    We can enter into the newly created Logic app by clicking the Go to resource button in the notification.



  • In Logic app Designer home screen, we can see two sections.

    Trigger selection section and Template selection section.

    In trigger selection section, we can see few commonly used triggers. Similarly, we can see different types of commonly used templates under template selection section.

    Please refer to the below screenshot to get an idea about different types of triggers and templates. From its name, we can understand its purpose.



  • I am going to select the Recurrence trigger for our logic app. As its name implies, this trigger is used to repeat an event after a time period.

    We can set the interval and frequency as below screenshot.



    For this logic app, I've selected 5 minutes as the interval-which means our logic app will trigger every 5 minutes

  • Click the New step button to add a new action.

    We need to connect the database to read newly added records with mailSent column contains 0

    To do this, we need to add the SQL server. So type SQL in search column and select the SQL server.



  • Then choose the Getrows(V2) action from list. This action is used to read rows from the database tables.



  • We will get the below screen to fill the server details 



    Select the SQL server name from the drop-down box. It will show all available SQL servers in our Azure account

    Once the server is selected we can select the database from the next dropdown box. This will list all databases present in the selected SQL server.

    From the third dropdown box we can select the table.



  • We only need to select the records with mailSent status column contains value 0 (mail not send)

    To filter this data, we can add one more parameter by clicking the Add new parameter text box in the above screenshot.

    Then select the Filter Query item from the list.



    Then provide the filter as mailSent eq 0 . 



    At this point our logic app is capable of fetching the user records from our database every 5 minutes.

    Next we need to loop through the records, set and send mail to each user.

  • Add new action and search for for each loop



  • Click the Select an output from previous steps text box.

    Now we can see the value from the previous step. Select the value from Dynamic content window.



  • Then click the Add an action button.



  • Next, we need to add mail service to send mail. Here I am using my gmail account to send mail.

    Search for gmail and select Gmail action as below.



    Then select the Send mail action from the list.



    This will prompt us to provide the Gmail account credential and permission to use the gmail. This will be a one time task.

  • Next, we need to set the email configuration like To, subject, body, etc.

    Click the To text box. It will list all columns in the data set value in the Dynamic content window. Click the see more link to see all columns.



    This will list all columns available in the data set.

    From the list select the usermail column.



  • Then, we need to add the subject and body for the mail.

    To add these fields, click the Add new parameter text box.

    Select the subject and body items from the list.



  • Click the subject text box and select the prblm_description column from the Dynamic content window.



  • Provide some content for the body section as below.



  • Save the logic app by clicking the save button. 



    Finally, we created our Azure logic app successfully  

    Let's run our logic app by clicking the Run button and check the mailbox(I provided my other mail id as the usermail value in database).

    We can see our logic app mail as below . 



  • Next, we should update the database by changing the mailSent column value to 1.Otherwise same user details will be read from database and send mail again.

    To update the database back, we can add one more action by clicking the Add an action button in the loop action.



  • Then, select the SQL Update row(V2) action 



  • Select the SQL server ,database,table from corresponding dropdown boxes.

    For Row Id column, select the primary key column name from the dynamic content window



  • Click the add parameter text box and select the mailSent column from the list



  • Give the value of the mailSent text box as 1. This value will be used to update the mailSent column in the database table.

That's All !! 
 
All Completed 
 
Save our logic app and trigger it again. We will receive the same mail as earlier. 
 
Check the table in the database. mailSent column will be updated to 1. During the next run this rows will not be considered.
 
Once tested the logic app, disable it by clicking the Disable button in the Logic app home screen. Otherwise it will trigger every 5 minutes.

No comments:

Post a Comment