๐ Variable Cascading in Power Automate with Example
- Variable cascading in Power Automate refers to the sequential use of variables where each subsequent variable's value depends on the previous one.
- This approach allows you to break down complex workflows into manageable steps, enhancing readability and maintainability.
- Below is a practical example demonstrating this concept:
๐ฐ Example: Order Processing Workflow
- Scenario: Automate order processing by validating customer eligibility, calculating discounts, and generating a final invoice. Each step uses variables that depend on prior results.
๐ช Step 1: Trigger and Initialize Variables
* ๐ **Trigger:** "When a new item is added" to a SharePoint list (e.g., *Orders* list).
* ๐งฐ **Initialize Variables:**
* ๐ CustomerID: Store the customer ID from the trigger.
```
Name: CustomerID
Type: String
Value: @{triggerBody()?['CustomerID']}
```
* ๐ต OrderTotal: Capture the raw order total.
```
Name: OrderTotal
Type: Float
Value: @{triggerBody()?['Amount']}
```
๐ช Step 2: Fetch Customer Details
* ๐ง๐ค๐ง Get Customer Tier:
* Use "Get item" (SharePoint) to fetch the customer’s membership tier using *CustomerID*.
* ๐️ Store the result in a variable:
```
Name: CustomerTier
Type: String
Value: @{outputs('Get_Customer_Details')?['body/Tier']}
```
๐ช Step 3: Calculate Discount Based on Tier
* ๐ธ Set Discount Rate:
* Use a Condition to determine the discount:
```
If CustomerTier = "Gold", set DiscountRate = 0.15
Else if CustomerTier = "Silver", set DiscountRate = 0.10
Else, set DiscountRate = 0.05
```
* ๐ฐ Initialize Variable:
```
Name: DiscountRate
Type: Float
Value: @{body('Condition')}
```
๐ช Step 4: Compute Final Amount
* ๐งพ Calculate Final Total:
* Set Variable:
```
Name: FinalAmount
Type: Float
Value: @{mul(variables('OrderTotal'), sub(1, variables('DiscountRate'))}
```
๐ช Step 5: Generate Invoice
* ๐ Create Invoice:
* Use "Create file" (OneDrive) to save the invoice with dynamic content:
```
File Name: Invoice_@{variables('CustomerID')}.txt
Content:
"Customer: @{variables('CustomerID')}
Order Total: $@{variables('OrderTotal')}
Discount: @{mul(variables('DiscountRate'), 100)}%
Final Amount: $@{variables('FinalAmount')}"
```
๐ Key Points
* **Cascading Dependencies:**
* CustomerID → Used to fetch CustomerTier.
* CustomerTier → Determines DiscountRate.
* DiscountRate + OrderTotal → Compute FinalAmount.
* **Benefits:**
* ๐งฉ Modularity: Each step is isolated and reusable.
* ✅ Clarity: Variables make the flow self-documenting.
* ๐ง Flexibility: Easily update discount rates or logic without disrupting the entire flow.
๐ Advanced Use Case: Multi-Level Approvals
* **Scenario:** Escalate approval requests based on dynamic thresholds.
* **Initialize Variables:** ApprovalLevel = 1, MaxApprovalLevel = 3.
* **Loop:** While ApprovalLevel <= MaxApprovalLevel:
* Send approval to the corresponding team.
* If rejected, increment ApprovalLevel.
* If approved, exit loop.
✨ Best Practices
* ๐ท️ Descriptive Names: Use clear variable names (e.g., DiscountRate, not var1).
* ๐งฑ Scope Management: Use Scope actions to group related variables.
* ๐จ Error Handling: Add checks for null/empty variables.
๐ By cascading variables, you create ๐ dynamic, adaptable workflows that mirror real-world business logic. This method is particularly useful for financial calculations, approval chains, and data transformations. ๐
No comments:
Post a Comment
Note: only a member of this blog may post a comment.