What are Azure functions? Example of Invoking Azure functions inside the Logic App.
Azure Functions
In General, functions are used to run the small pieces of code to make it reusable. Similarly, Azure functions are used to run the small pieces of code inside Azure platform. You can create the functions either in Azure portal (using function App) or in visual studio. Azure functions are serverless computing service hosted inside azure cloud. Serverless gives the capability to user to write the code and run it with any trigger or event. Azure functions are not complete, fully featured applications instead they handle specific, short lived tasks. Azure functions supports functions developed in C#, F#, php, python and many other.
Azure functions also support Nuget open source package manager and many other libraries, continuous development, deploy code through Git, and also provides security through OAuth systems etc.
Benefits of using Azure functions
1. Quick development and deployment of small components to Azure functions.
2. Directly code your functions right from your Azure Management Portal.
3. Cost effective, pay only for the execution minutes of your applications
Basic Example of using function App in the Logic App
Function Apps are used to create azure functions. Now, we can use this function in any azure application or service. In the following example, I am using this function in logic app. We are using the salesforce connector for synchronizing Salesforce and Azure Database, for this we created a trigger on salesforce object “Contact” and specified the azure function “GetName” as our trigger action. We have an azure SQL database named “SalesforceData” (which includes the Table Contact based on which my function App is designed) as shown in the below screenshot :
Target here is, Whenever a new Contact will be created at the Salesforce end, GetName function from the Azure function app will be executed and we will insert the Contact record in the Azure SQL table Contact, in other words returned response as a string Name from function will get inserted in the firstName field of table if the status code is 200.
First, Create a function App in Azure Portal (let’s say FunAppDemo). After creating the function app, It will open the following screen.
Click on Functions tab + icon in its right to create a new function. It will open the following screen:
Select the function type i.e. which type of trigger will run this function. I have selected the Http trigger for this example. Click on http trigger and add the appropriate details (desired language, your trigger name, desired authorization level) as shown in following screenshot.
Click on the create button and it will create the following function in which we are accepting a name parameter in the http request url and this function is returning the value of that name parameter in the response with ok status if name is not null otherwise returning the response as a ‘Bad Request’ along with a message asking for passing a value in the name parameter.
Screenshot below shows the processing Logic inside the ‘GetName’ function –
Now, we can invoke this function in any azure application or service. In this example, I am invoking this Azure function inside the logic app as an action when the trigger ‘When a record is created’ is fired as shown in the following screenshot:
We will specify the http request parameter as shown in the following screenshot:
Now I will specify another action inside the logic app that will insert the Contact record in the Azure SQL table Contact in which response i.e. Name parameter from function will get inserted in the firstName field of table instead of salesforce Contact FirstName field. For this we will specify the input values for the table (First Name field set as response returned from function app and other field values selected as response values from Salesforce) as shown below in the screenshot:
Now, save the logic App and click on the Run button.
Create the new record in salesforce Contact object as shown in following figure:
Now, check the logic App designer and see if your logic app ran successfully.
You can check the details of every step by clicking on them as shown in following screenshot:
Now, check the SQL db table whether record data is inserted properly or not.
This example is very simple and for demo purpose only. You can create as complex functions as required and can be called by any other azure service like Azure Event Hub, generic webhook, logic apps etc.