Cosmos DB Document Approval State Machine
When a document enters a pending-review state in Cosmos DB, the flow runs a Teams/Outlook approval, updates the document's status and approver fields on decision, and triggers the next step or notifies the submitter. Adds a governed approval workflow over Cosmos-stored business documents.
Provided as-is, without warranty of any kind. Review and test each pattern in a non-production environment before deploying it to live automations. See our Terms.
Overview
This flow adds a governed approval workflow over business documents stored in Azure Cosmos DB. On an hourly schedule it finds every document whose status field equals a pending-review value, runs a Microsoft Approvals (Teams/Outlook) approval for each one, writes the decision back onto the document (status, approver, decision date, correlation id) via an upsert, and posts the outcome to a Microsoft Teams channel.
Why it matters: Cosmos-backed apps usually have no built-in approval/sign-off mechanism. Power Automate layers a routable, auditable approval state machine over the documents without changing the application - the document itself carries its own workflow state. Shipped Off as a reference implementation; going live requires only authorizing the three connections and setting the six environment variable values.
Use Case
A Cosmos-backed application stores business documents (orders, content items, change requests, etc.). Each document carries a status field. When a document is set to a pending-review value, it must be approved by a designated reviewer before it can proceed. This flow polls for those pending documents, gets a decision, persists it back onto the document, and notifies the team - turning the status field into a durable approval state machine.
Flow Architecture
Recurrence
Recurrence (Hour / 1)Hourly poll of Cosmos DB for documents awaiting review.
Initialize correlation id
Initialize Variable - guid()Mints a run-wide trace id stamped on the approval request, both Cosmos write-backs, and both Teams notifications so a run can be traced end-to-end.
Initialize configuration variables
Initialize Variable (env vars + configurable labels)Cosmos account/database/container, approver email, Teams group/channel, status field name plus state labels (PendingReview/Approved/Rejected), and title/submitter field names.
Compose Cosmos query
ComposeBuilds SELECT * FROM c WHERE c[status] = PendingReview from the status field and pending value.
Query pending documents
Azure Cosmos DB - QueryDocuments_V5Returns all pending documents under body/value.
Apply to each pending document
ForeachRuns the approval state machine for each pending document.
Start and wait for approval
Approvals - StartAndWaitForAnApproval (Basic)Sends an Approve/Reject card to the approver and waits for the decision.
Branch on approval outcome
ConditionTests body/outcome equals Approve.
Write decision back to Cosmos
Compose setProperty + Azure Cosmos DB - CreateDocument_V3 (upsert)Sets status to Approved or Rejected plus approver, decisionDate and correlationId on the original document, then upserts it back under the same id.
Environment Variables
| Schema name | Type | Default | Description |
|---|---|---|---|
| flowlibs_CosmosAccountName | String | flowlibs-cosmos | Cosmos DB account name |
| flowlibs_CosmosDatabaseId | String | ReferenceDb | Cosmos database id (point at the database holding the documents) |
| flowlibs_CosmosContainerId | String | ReferenceData | Cosmos container/collection id holding the documents |
| flowlibs_ApprovalDefaultApproverEmail | String | approver@example.com | UPN/email the approval is assigned to |
| flowlibs_TeamsGroupId | String | <configure> | Teams team (group) id for notifications |
| flowlibs_TeamsChannelId | String | <configure> | Teams channel id for notifications |
Connectors & Connections
| Connector | API name | Actions used |
|---|---|---|
| Azure Cosmos DB | shared_documentdb | QueryDocuments_V5 CreateDocument_V3 (upsert write-back) |
| Approvals | shared_approvals | StartAndWaitForAnApproval (Basic) |
| Microsoft Teams | shared_teams | PostMessageToConversation |
Note — All connections are referenced as solution connection references; the flow is portable between environments as long as a connection is mapped at import time.
Customization Guide
Almost every realistic variant of this flow can be implemented by changing environment variable values. A few cases require small edits inside the flow definition — those are called out explicitly below.
- Different status model
- Change varStatusField, varPendingValue, varApprovedValue and varRejectedValue to match your document schema; the Cosmos query and write-backs follow automatically.
- Multi-stage approval
- Chain a second StartAndWaitForAnApproval inside the approved branch for sequential sign-off.
- Avoid re-polling in-flight documents
- Set an interim status (e.g. InReview) on the document before starting the approval so the next hourly poll skips it.
- Timeout / escalation
- Add a parallel Delay plus reminder branch to auto-escalate stale reviews.
- Optimistic concurrency
- Pass the document _etag on the upsert (If-Match) to prevent lost updates under concurrent edits.
- Notify the submitter directly
- Extend the Teams message (or add an Outlook SendEmailV2) using the varSubmitterField value captured from each document.
Key Expressions
The flow is intentionally light on Power Fx / WDL gymnastics — the heaviest expressions are the branch-name concatenation and the approval outcome check. They are listed below in the order they appear in the flow.
EXPR.01Cosmos query
Builds the pending-documents query from the configurable status field and pending value.
EXPR.02Approval outcome
Equals Approve when the reviewer approves.
EXPR.03Approved write-back body
Rebuilds the document preserving all fields and updating only the workflow fields.
EXPR.04Dynamic document field read
Reads a document field by its configurable name.
EXPR.05Correlation id
Minted in Initialize_varCorrelationId.
Comments
Sign in to join the conversation.
Sign inNo comments yet. Be the first to share your experience with this flow.