# Power Automate Expressions & Deterministic Governance Patterns

This reference documents the production-tested standard expressions used across **EnterpriseDemo-CBPA** (100% Microsoft 365 Standard Connectors, zero premium licensing).

---

## 1. Collision-Free Unique Title Placeholder (`Flow 1`)
Prevents SharePoint `EnforceUniqueValues` collisions when multiple records are initiated concurrently before retrieving the auto-increment SharePoint `ID`.

**Create item (`Title`):**
```text
@{concat('TEMP-', triggerOutputs()?['body/resourceData/responseId'])}
```

**Update item (`Title` & `SubmissionKey`):**
```text
Title:         @{concat('DEMO-2026-', formatNumber(outputs('Create_item')?['body/ID'], '0000'))}
SubmissionKey: @{toUpper(substring(guid(), 0, 8))}
```

---

## 2. Null-Safe Partial-Edit Type Casting (`Flow 1` & `Flow 2A`)
Power Automate evaluates both branches of `if()` eagerly. Wrapping the inner value prevents `int('')` or `float('')` runtime failures when regional staff leave unchanged fields blank during a returned partial edit.

**Integer Field (e.g., Attendance / Late Minutes):**
```text
@if(
  empty(trim(coalesce(outputs('Get_response_details')?['body/<NUMERIC_QUESTION_ID>'], ''))),
  items('Apply_to_each')?['LateMinutes'],
  int(if(empty(trim(coalesce(outputs('Get_response_details')?['body/<NUMERIC_QUESTION_ID>'], ''))), '0', trim(outputs('Get_response_details')?['body/<NUMERIC_QUESTION_ID>'])))
)
```

**Decimal Field (1 Decimal Place Score `0.0–5.0`):**
```text
@if(
  empty(trim(coalesce(outputs('Get_response_details')?['body/<SCORE_QUESTION_ID>'], ''))),
  items('Apply_to_each')?['SelfQ01'],
  float(if(empty(trim(coalesce(outputs('Get_response_details')?['body/<SCORE_QUESTION_ID>'], ''))), '0', trim(outputs('Get_response_details')?['body/<SCORE_QUESTION_ID>'])))
)
```

---

## 3. Authenticated Manager Authorization Gate (`Flow 2B`)
Combines Status Gate, Record Matching (`SubmissionKey`), and Entra ID responder validation so only the designated HQ line manager can score the assigned employee.

```text
@and(
  equals(items('Apply_to_each')?['Status/Value'], '2_ManagerScoringPending'),
  equals(toUpper(trim(outputs('Get_response_details')?['body/<KEY_QUESTION_ID>'])), items('Apply_to_each')?['SubmissionKey']),
  equals(toLower(trim(outputs('Get_response_details')?['body/responder'])), toLower(trim(items('Apply_to_each')?['AppraiserEmail'])))
)
```

---

## 4. Concurrency-Safe Batch Completion Gate (`Flow 3`)
Combined with Trigger Settings `Concurrency Control = On` (`Degree of Parallelism = 1`), ensures executive batch approval fires **exactly once** when all non-exempt staff complete HR review.

**Trigger Condition:**
```text
@and(
  equals(triggerOutputs()?['body/Status/Value'], '5_HRReviewPending'),
  equals(triggerOutputs()?['body/HRApproved'], true)
)
```

**Batch Gate Condition (comparing `Get_All_Active` vs. `Get_Ready_Batch`):**
```text
@and(
  equals(length(outputs('Get_All_Active')?['body/value']), length(outputs('Get_Ready_Batch')?['body/value'])),
  greater(length(outputs('Get_Ready_Batch')?['body/value']), 0)
)
```

---

## 5. Styled HTML Summary Table for Executive Approvals (`Flow 3`)
Replaces heavy Excel-to-PDF pipelines with a clean, mobile-friendly HTML table embedded inside M365 Approvals and linked to the permission-isolated SharePoint view.

```text
@{replace(
  replace(
    body('Create_HTML_table'),
    '<table>',
    '<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;font-family:Segoe UI,Microsoft JhengHei,sans-serif;font-size:13px;width:100%;">'
  ),
  '<thead>',
  '<thead style="background-color:#1F4E79;color:#FFFFFF;">'
)}
```
