Understanding expressions in Logic Apps | Frequently used expressions in Logic Apps | What is expressions in Logic App

{tocify} $title={Table of Contents}


Introduction

It is said that No code knowledge is required to work with Logic Apps.

It is true to major extent, however there are scenarios where we need to go for custom code and for that we make use of Azure Function (it's a separate service and not part of logic App ).

Read about to understand Azure Function - Azure Function Basics

However, Expressions are part of Logic Apps and requires a bit of coding.



What is expressions in Logic Apps

Expressions in logic apps are statements which are used to manipulate data,  perform calculations and evaluate conditions with the help of functions provided by the workflow definition language. 


In a layman term

Expression = utilizing one or more functions with required parameters which executes and ends up being a  value.

You can use expressions to perform a variety of tasks in Logic Apps. Here are some examples:

Manipulate data:
Expressions can be used to manipulate data in many ways e.g. you can use them to convert data types, extract substrings, or remove characters.

Decision making:
Expressions can be used to make decisions within workflows e.g. you can use this to check if a value is greater than or equal to a certain threshold, or to determine if a string contains a certain word. 

Control the flow of your workflow:
You can use complex expressions to control the flow of your workflow e.g. you can use them to branch your workflow based on the result of an expression, or repeat a section of your workflow a specified number of times.

Expressions can be powerful tools for automating tasks and controlling the flow of Logic Apps workflows. However, it is important to use it carefully to avoid mistakes. 

When creating expressions, it is important to carefully consider the logic of the expression and use the correct functions and operators. It's also a good idea to test expressions thoroughly before using them in workflows.




Functions available in Logic App


There are different types of functions available which can be used in expressions in your logic apps, including:

Logical comparison functions:
These functions are used to compare values ​​and evaluate conditions such as 'and', 'equals', 'greater than', 'if', 'lessOrEquals' and 'not'.

Conversion functions:
These functions are used to convert values ​​from one type to another like Bool, Float, Int, String, XML, JSON, etc.

Mathematical functions:
These functions are used to perform mathematical operations on numbers such as: Examples: add, div, mod, mul, sub, etc.

Date and time functions:
These functions are used to manipulate date and time values like addDays, addHours, ConvertFromUtc, ConvertTimeZone, utcNow, etc.

String functions:
These functions are used to manipulate strings such as concat, replace, substring, toLower, toUpper.

Collection functions:
These functions are used to manipulate arrays, strings and dictionaries  Includes first, last, length, union, etc. 

Workflow functions:
These functions are used to get information about the workflow or its components like actionsOutputs, Parameters, triggerBody, triggerOutputs, Workflow, etc. 

URI parsing functions:
These functions are used to parse and manipulate URIs or URLs e.g decodeUriComponent, encodeUriComponent, uriHost, uriPath, uriQuery, etc. 


Where are expressions used in Logic Apps


You can dynamically assign values ​​to parameters, variables, and properties of actions  based on logic and conditions with help of expressions.

You can use the Expression editor in the Logic App designer. The Expression editor provides a list of all the available functions and operators, as well as a preview of the results of your expression.

In the Designer or Expression Editor, you can use expressions to define the inputs or outputs of actions or triggers e.g. you can use an expression to specify the URL for an HTTP Request action or the body for an HTTP Response action.

In Code view, you can use expressions to define action or trigger properties that accept expressions e.g. you can use expressions to specify conditions for switch actions or repeat trigger intervals.

Execution history allows you to use expressions to evaluate the input or output of an action or trigger at runtime e.g. you can use expressions to check the status code of an HTTP response action or the value of a variable. 



Most frequently used expressions in Logic Apps



body('action_name') -  expression in Azure Logic Apps is used to get the output of an action. The action_name parameter is the name of the action that you want to get the output from. The output of an action can be a JSON object, a string, or a number.

if(condition, true_value, false_value) - Evaluates a condition and returns a value. The condition parameter is a Boolean expression that evaluates to true or false. The true_value parameter is the value that is returned if the condition is true. The false_value parameter is the value that is returned if the condition is false.

parameters('Parameter_name') - expression in Azure Logic Apps is used to get the value of a parameter.

workflow().run.name  - expression in Azure Logic Apps is used to get the name of the current workflow run. The workflow() function returns a workflow object, and the run property of the workflow object returns a run object. The name property of the run object returns the name of the current workflow run.

union(collection1, collection2, ...) - expression in Azure Logic Apps is used to combine two or more collections into a single collection.

item() - expression in Azure Logic Apps is used to return the current element in the array during the current iteration of the action. You can also get the value from the properties of that element.

trigger('event_name') - Gets the input of a trigger.

variables('variable_name') - Gets the value of a variable.

concat('string1', 'string2') - Concatenates two strings.

length('string') - Gets the length of a string.

trim('string') - Removes whitespace from the beginning and end of a string.

upper('string') - Converts a string to uppercase.

lower('string') - Converts a string to lowercase.

replace('string', 'old_string', 'new_string') - Replaces a substring in a string.

split('string', 'delimiter') - Splits a string into an array.

join(array, 'delimiter') - Joins an array into a string.

empty('string') - Checks if a string is empty.

and(condition1, condition2) - Evaluates two conditions and returns true if both conditions are true.

or(condition1, condition2) - Evaluates two conditions and returns true if either condition is true.

not(condition) - Evaluates a condition and returns false if the condition is true.

base64ToString(triggerBody()) -  expression in Azure Logic Apps is used to decode a base64-encoded string.


formatDateTime(utcNow(), 'yyyy-MM-ddTHH:mm:ss') - expression in Azure Logic Apps is used to format a date and time value into a string as specified in second parameter

result('Action_name or Scope_name') - expression in Azure Logic Apps is used to get the result of an action

coalesce(value1, value2, value 3...) - expression in Azure Logic Apps is used to return the first non-null value from a list of values.


Bit complex expressions in Logic Apps


Complex expressions in Logic Apps are expressions that use multiple functions and operators to perform a task i.e. nesting function in another, few examples below:

json(base64ToString(triggerBody())) expression in Azure Logic Apps is used to decode a base64-encoded string and then convert it to JSON.

replace(replace(replace(replace(replace(json(body('Action_name')),'-','_'),'+','_'),' ','_'),' - ','_'),'- ','_')  -  expression in Azure Logic Apps is used to replace a substring (multiple) within a string. 



To write expressions in Logic App, you can use the following steps:

  • In the Logic App designer, open the action or trigger where you want to add the expression.
  • In the Expression box, type the expression.
  • To use a variable in the expression, prefix the variable name with the $ sign.
  • To use a function in the expression, prefix the function name with the @ sign. Function parameters are evaluated from left to right. If any of the parameter is optional then use question mark(?).
  • To use a property of an object in the expression, prefix the property name with the [ sign.
  • To use a value in the expression, type the value.
  • To evaluate the expression, press Enter.


Learn More about Logic App

Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment

Previous Post Next Post