How to Build a Self-Correcting Daily Census in Salesforce

When an admit date, discharge date or facility changes, one Salesforce flow adds or removes that patient on every affected day. Five of the six cases are built.

By Published

A multi-facility behavioral-health center asked us to fix one number: today's census. Admissions corrected dates all the time (an admit date entered wrong, a discharge recorded late, a stay moved to another facility), and each correction left the counts for days already reported wrong. Someone rebuilt the spreadsheet every morning. We replaced it with the flow on this page, built in Flow Builder with no Apex, and handed the client's team the documentation to own it. What follows is that design with the client's name and data removed.

What data does a Salesforce daily census need?

Two objects. The stay lives on the Opportunity, because admissions already works it there. The census lives on its own object with one row per facility per calendar day, so a report for any date range is a sum of rows nobody has to recalculate.

The census data model
ObjectFieldWhat it holds
Opportunity (the stay)Admission_Date__cThe first day of the stay
Discharge_Date__cThe day the patient leaves; blank while the stay is open
AccountIdThe facility, held as an Account
Outcome__cThe admission's status, including Excluded
Daily_Census__cCensus_Date__cThe calendar day
Facility__cThe facility the row counts
Census_Count__cPatients in that facility on that day

Which changes does the census have to correct?

Six. Each one touches a different set of days, and each adds or subtracts exactly one patient on each day it touches. The examples use the counting rule in the flows as built: a patient counts from the admit day through the day before discharge.

The six census corrections
Change on the stayDays it touchesCountExampleStatus
Admit date moves earlierNew admit date to the day before the old one+1Jan 15 → Jan 10 adds Jan 10 to 14Built
Admit date moves laterOld admit date to the day before the new one-1Jan 10 → Jan 15 removes Jan 10 to 14Built
Discharge date moves earlierNew discharge date to the day before the old one-1Jan 20 → Jan 15 removes Jan 15 to 19Built
Discharge date moves laterOld discharge date to the day before the new one+1Jan 15 → Jan 20 adds Jan 15 to 19Built
Facility changesEvery day of the stay, at both facilities-1 old, +1 newFacility A → B: A loses the stay, B gains itBuilt
Outcome changes to ExcludedEvery day of the stay-1, or +1 if reversedAn admission later excluded from censusDesigned, not built

The stay as entered: admit Jan 15, discharge Jan 20

Admit date corrected to Jan 10: the flow adds 1 on Jan 10 to 14

Discharge corrected to Jan 18: the flow subtracts 1 on Jan 18 and 19

Counted after both edits: Jan 10 to 17

One stay, January 9 to 20, corrected twice. Each edit rewrites only the days between the old date and the new one; Jan 9, Jan 20 and every day outside the edits keep their counts.

How does the flow know what changed?

The master flow, Opportunity: Recalculate Daily Census, is record-triggered and runs after an Opportunity is saved. It starts when the admit date, the discharge date or the facility changes on a stay that already had a value there, or when the outcome changes to Excluded. The old values come from $Record__Prior, which Salesforce fills with the record's values from immediately before the save (Global Variables Resource, Salesforce Help, read 16 October 2026).

Two details in that start condition decide what the census can see. The prior value has to exist, so the flow corrects a date that moves and ignores a date entered for the first time. And Salesforce documents that when a record is created and updated in the same transaction, $Record__Prior holds the same values as $Record. An import that inserts a stay and fixes its dates in one transaction changes nothing in the census, so load corrections as a separate update.

A decision then sends each change to its subflow. Salesforce evaluates a Decision element's outcomes in the order they are listed and takes one path (Decision Element, Salesforce Help). A single decision with an outcome per case would correct only the first change in a save that moves both dates. Chain one decision per field so every change reaches its subflow, and test a two-field save; it is on our test list for that reason.

What does each census subflow do?

Every subflow has the same five parts, which is what lets an admin read one and understand all of them:

  1. Inputs: the facility and the two dates that bound the change, old and new, passed from $Record and $Record__Prior.
  2. Get Records: the Daily_Census__c rows for that facility from the first day affected up to the last.
  3. Loop with an Assignment: add or subtract 1 on each row and add the row to a collection.
  4. One Update Records after the loop: write the whole collection at once.
  5. A fault path: if the update fails, notify an admin with the stay and the error.

The update sits after the loop for a reason. Salesforce allows 150 DML statements and 10,000 changed records per transaction, and when an element exceeds a limit it rolls back the whole transaction, even if that element has a fault path (Per-Transaction Flow Limits, read 16 October 2026). An Update Records element inside the loop spends one statement per day, so correcting a 30-day stay uses 30 of the 150 on one patient. After the loop it uses one.

Each subflow also stops when it finds no rows. That keeps the corrections safe to run, and it means a day with no census row for that facility is never corrected. Whatever creates each day's rows has to run before a correction can land on that day.

How is a facility transfer counted?

The transfer subflow makes two passes over the same dates. It subtracts 1 from the old facility for every day of the stay, then adds 1 to the new facility for the same days. Both passes use the stay's dates, so the stay counts as if the patient had been at the new facility the whole time. That is right for a stay entered at the wrong facility. If your patients move mid-stay and the days before the move belong to the first facility, add a transfer date to the stay and bound each pass by it.

Behavioral Health Integration Fit Check

Get a census leadership can read without a morning rebuild.

What happens when an admission is excluded from census?

The exclusion subflow is designed and documented. When the outcome changes to Excluded, it subtracts 1 for every day of the stay at that facility; when an exclusion is reversed, it adds those days back; any other outcome change does nothing. Our handover diagram for this build marks it as not yet implemented, so in that build an exclusion does not correct the census on its own. It is the next subflow to build, and it follows the same five parts as the others.

Which days count: the admit day, the discharge day, or both?

Decide the counting rule before building and use it in every subflow. The flows as built count a patient from the admit day through the day before discharge, which is why moving a discharge from Jan 20 to Jan 15 removes Jan 15 to 19. Counting the discharge day as well shifts every range by one day, and a subflow written to the other rule quietly drifts the count. Two more rules belong in the same decision: what an open stay counts through (our test plan counts a stay with no discharge date through today), and whether a same-day admit and discharge counts at all, which under this rule it does not.

What should you test before trusting the census?

  1. Each correction on a stay with known dates, checking the count on every day of the range and one day either side.
  2. A facility change: the old facility drops by one and the new one gains one on every day of the stay.
  3. An open stay with no discharge date.
  4. A same-day admit and discharge.
  5. A save that changes two fields at once: both dates, or a date and the facility.
  6. A bulk update of 20 or more stays in one save, to confirm the transaction stays inside its limits.
  7. A forced failure, to confirm the admin notification arrives.

Where does the census sit next to the EHR?

In this design the census is Salesforce work. The EHR keeps the clinical chart; admissions, census and follow-up run in Salesforce, where leadership reads them, and the census reads the same stay record admissions uses. For the EHR side, see what syncs between AdvancedMD and Salesforce and the Opus admission pipeline. The other systems we run for treatment centers are on our behavioral health page.

Frequently asked questions

Can Salesforce keep a daily census for a treatment center?

Yes. One custom object holds a row per facility per day, and record-triggered flows keep those rows current when a stay changes. The build on this page uses core Salesforce Flow with no Apex and no managed package, so the client's admin maintains it.

Why not recount the whole census every night?

A nightly recount is simpler to reason about and works at small volumes. It also rewrites every row every night, has to fit inside per-transaction limits as stays accumulate, and leaves yesterday's numbers wrong until it runs. Incremental corrections touch only the days a change affects, at the moment the change is saved.

What happens if a census correction fails?

A handled error follows the subflow's fault path, which notifies an admin; the stay edit is saved and the admin corrects that date range. An error that exceeds a governor limit rolls back the whole save, including the stay edit, and the person who saved sees it fail.

Does a self-correcting census work across several facilities?

Yes. Every census row carries its facility, every subflow filters by it, and a facility change moves the stay from one facility's rows to the other's.

Is the exclusion correction live?

Not in the build described here. It is designed: subtract the stay when the outcome changes to Excluded, and add it back if the exclusion is reversed. Our handover documentation marks it as not yet implemented.

Continue the decision

View all resources →

Behavioral Health Integration Fit Check

Get a census leadership can read without a morning rebuild.

Free, 30 minutes, no obligation.

  • Fit Check readout
  • A probable integration shape for the workflow
  • API, access, and system-of-record risks to verify
  • A recommendation: vendor discovery, paid Blueprint, or no custom build