Invoice Approved Milestone Event¶
An accounts payable manager wants a live spend dashboard: approved invoice totals by vendor and by day. The numbers already exist on each work item at the moment of approval. A Custom Event step on the approved path records them once, in the team's own Cosmos DB, where the dashboard reads them.
At a glance
- Extension point Custom Event step
- You need A Secondary Event Provider with Custom ticked in Filter Events; work-item fields
Index.InvoiceNumber,Index.Vendor,Index.InvoiceTotal,Index.Currency; permission to edit the workflow. - Teaches The basic event, clean string values, and a dashboard query over the event store.
Set it up¶
- Check the provider. An administrator opens Configuration > Apps > Event Settings, edits the provider, and confirms that Custom is ticked on Filter Events.
- In the Workflow designer, open the invoice workflow. Find the route that leaves the approval step for the approved path.
- Drag Custom Event from Automated Steps onto that path, after the approval and before Upload To Destination. Name it "Invoice Approved Event".
- Set Scopes to
invoice, approved. - Paste the script below into JavaScript Block. Save the step and the workflow.
Script¶
This recipe assumes one invoice per work item, with the values at work-item level. For several invoices in one work item, see Variations.
// Custom Event "Invoice Approved Event". Scopes: invoice, approved
// Place on the approved path, after the approval step.
// Reads work item Index.InvoiceNumber, Index.Vendor, Index.InvoiceTotal, Index.Currency.
// Writes one event: custom, invoice, approved.
function clean(value) {
return String(value || "").trim();
}
function toAmount(value) {
var n = parseFloat(String(value || "").replace(/[^0-9.\-]/g, ""));
return isNaN(n) ? "" : n.toFixed(2);
}
var idx = batch.Metadata["Index"] || {};
builder
.SetEventProperty("invoiceNumber", clean(idx["InvoiceNumber"]))
.SetEventProperty("vendor", clean(idx["Vendor"]))
.SetEventProperty("invoiceTotal", toAmount(idx["InvoiceTotal"]))
.SetEventProperty("currency", clean(idx["Currency"]).toUpperCase() || "USD")
.SetEventProperty("documentCount", String(batch.Documents.length));
How it works¶
- The step adds
customin front of the Scopes, so the event iscustom, invoice, approved. The dashboard filters on those three words. toAmountstrips$and,and writes two decimals, so the store holds"1520.40", not"$1,520.40".- Every property is a string. A clean number casts in the query.
cleanturns a missing value into"". The key always exists, so the dashboard never has to test for a missing field.documentCountusesString(...), because a property value must be a string.- The step does not change the work item. It queues the event and moves the work item to the next step.
The event document¶
The event the dashboard reads (Cosmos DB, ids shortened):
{
"EventId": "c1f2a9d4-...",
"Scopes": ["custom", "invoice", "approved"],
"OccurredTime": 1791380000000,
"Timestamp": "2026-10-07T13:33:20Z",
"WorkItemId": "8a41f0c2-...",
"ProcessId": "2f6b11e0-...",
"ProcessVersion": 12,
"StepId": "e93c5a77-...",
"WorkType": "Custom Event",
"Properties": {
"invoiceNumber": "INV-20931",
"vendor": "Midwest Pallet & Crate Co.",
"invoiceTotal": "1520.40",
"currency": "USD",
"documentCount": "3"
}
}
The full envelope (Id, PartitionKey, SubscriptionId, MachineId and the rest) is on the Custom Event step page.
Dashboard query¶
A Cosmos DB query for the dashboard's "approved spend by vendor" tile:
SELECT c.Properties.vendor AS vendor,
SUM(StringToNumber(c.Properties.invoiceTotal)) AS approvedTotal,
COUNT(1) AS invoices
FROM c
WHERE c.Scopes[0] = "custom" AND c.Scopes[1] = "invoice" AND c.Scopes[2] = "approved"
AND c.Properties.invoiceTotal != ""
GROUP BY c.Properties.vendor
For a daily trend, group by the date part of Timestamp in your reporting tool.
Test it¶
- Run one test invoice through approval on a non-production tenant. Note its work item id in Monitor or Insight.
- In the Cosmos DB
Eventscontainer, runSELECT * FROM c WHERE c.WorkItemId = "<work item id>" AND c.Scopes[0] = "custom". - Check the five properties against the work item's
Indexvalues.
You should see
one document with Scopes ["custom", "invoice", "approved"], WorkType Custom Event, and Properties.invoiceTotal as a plain number string such as "1520.40". The work item continues to the next step with no change to its metadata.
What can go wrong¶
Watch out
- No document arrives. Custom is unticked on the provider's Filter Events, or the tenant has no provider. Delivery is in the background, so the workflow shows no error.
- Two documents for one invoice. The work item passed the step twice, for example after it was sent back for correction. Count distinct
WorkItemIdin the dashboard, or keep the latest event perWorkItemId. invoiceTotalis empty. The total was missing or had no digits. Fix extraction or indexing. The dashboard query skips empty totals.- Totals in several currencies add up wrong. Group by
currencytoo, or convert before approval. - The vendor appears under two names. The index value differs in spelling or case. Normalize it with a lookup before approval. See Fill Fields from a Lookup Table.
Variations¶
- Several invoices in one work item. Send one event with the work item's total and count, plus a JSON list of the invoices:
var lines = [];
var sum = 0;
for (var i = 0; i < batch.Documents.length; i++) {
var d = (batch.Documents[i].Metadata["Index"] || {});
var n = parseFloat(String(d["InvoiceTotal"] || "").replace(/[^0-9.\-]/g, "")) || 0;
sum += n;
lines.push({ invoiceNumber: d["InvoiceNumber"] || "", total: n.toFixed(2) });
}
builder
.SetEventProperty("invoiceTotal", sum.toFixed(2))
.SetEventProperty("invoiceCount", String(lines.length))
.SetEventProperty("invoices", JSON.stringify(lines));
- Rejected invoices. Put a second Custom Event step on the rejected path with Scopes
invoice, rejectedand add areasonproperty. One dashboard then shows both outcomes.