Error Handling with Expressions in Power Automate
β οΈ Error Handling with Expressions in Power Automate
Error handling with expressions in Power Automate is crucial for building resilient flows that can gracefully respond to issues like missing data, failed actions, or null values. Expressions let you anticipate, catch, and respond to runtime errors β without interrupting the entire flow.
With the right combination of functions and logic, you can catch potential errors, return fallback values, or route the flow differently to avoid failure.
π Why Error Handling Matters in Power Automate
Power Automate flows may break due to:
- Missing or invalid data
- Null or undefined values
- API failures or timeouts
- Division by zero or string parsing issues
- Skipped or failed actions
Using error handling with expressions in Power Automate, you can:
- Prevent flows from terminating unexpectedly
- Display user-friendly error messages
- Route errors to alternate logic branches
- Log issues to SharePoint, email, or Dataverse
π§° Error Handling Expression Functions Reference
Function | Description | Example | Result |
---|---|---|---|
coalesce(a, b) |
Returns the first non-null value | coalesce(null, 'Default') |
'Default' |
if(condition, true, false) |
Conditional check | if(equals(1,1),'OK','Fail') |
'OK' |
equals(a, b) |
Checks for equality | equals(5,5) |
true |
empty(value) |
Checks if value is empty | empty(triggerBody()?['Email']) |
true/false |
contains(text, value) |
Checks if string contains value | contains('abc', 'a') |
true |
hasProperty(object, key) |
Checks if a property exists | hasProperty(triggerBody(), 'email') |
true/false |
tryCatch() (via scopes) |
Manual try-catch logic | β | Error-safe branching |
isNull(value) |
Returns true if value is null | isNull(null) |
true |
int() , float() , etc. |
Safe type conversion | int('abc') |
Error-prone unless checked |
πΌ Real-World Use Cases
Scenario | Expression | Purpose |
---|---|---|
Handle missing user email | coalesce(triggerBody()?['Email'], 'default@email.com') |
Prevent null failure |
Log failed step status | outputs('Get_Item')?['statusCode'] |
Detect 400/500 errors |
Route based on success/failure | if(equals(outputs('HTTP')?['statusCode'], 200), 'Success', 'Fail') |
Control flow |
Check if a value exists | hasProperty(triggerBody(), 'email') |
Prevent runtime errors |
Use fallback value | if(empty(triggerBody()?['Comments']), 'No comments', triggerBody()?['Comments']) |
Better UX |
π§ͺ Examples of Error Handling with Expressions in Power Automate
1. Fallback Email Address with coalesce()
coalesce(triggerBody()?['Email'], 'default@email.com')
Use Case: Prevents null reference if Email isnβt provided.
2. Detecting Empty Fields
if(empty(triggerBody()?['Notes']), 'No notes available', triggerBody()?['Notes'])
Use Case: Prevents empty display values in approval emails.
3. Handling Action Failures Gracefully
if(equals(outputs('HTTP')?['statusCode'], 200), 'Success', 'API Failed')
Use Case: Log or respond differently based on API result.
4. Check for Missing Properties
if(hasProperty(triggerBody(), 'Manager'), triggerBody()?['Manager'], 'N/A')
Use Case: Prevents flow from breaking when a field is missing.
5. Avoid Division by Zero
if(equals(variables('Denominator'), 0), 'Undefined', div(variables('Numerator'), variables('Denominator')))
Use Case: Prevents runtime error when dividing numbers.
β Best Practices for Error Handling with Expressions
- β
Use
coalesce()
to provide safe default values - β
Always check for
null
orempty()
values before using them - β
Use
hasProperty()
before referencing dynamic properties - β
Wrap risky logic (like division or conversion) in
if()
statements - β Combine with Configure Run After to control action failures
- β Use Scopes to build try/catch logic with Success/Failure/Timeout branches
β οΈ Common Pitfalls and How to Avoid Them
Pitfall | Cause | Fix |
---|---|---|
Flow fails on null value | Directly accessing undefined fields | Use coalesce() or hasProperty() |
Unexpected output in emails | Empty strings or nulls in content | Use fallback logic with if() |
Breaking on failed actions | Not using Configure Run After |
Use failure paths in scopes |
Expression not evaluating | Wrong property path | Use Peek Code to verify structure |
Error on conversion | Invalid string in int() or float() |
Wrap with if() or validate first |
π Summary: Mastering Error Handling with Expressions in Power Automate
Use error handling with expressions in Power Automate to:
- Create fail-proof flows that donβt crash on invalid or missing data
- Provide clear, helpful messages or fallback outputs
- Log and trace failures to improve visibility
- Adapt flow behavior dynamically using metadata and condition checks