What this project covers
Triggers that react to record updates, batch classes, validation, wrapper classes for LWCs, email logic, Permission Sets, Flows and custom sharing — all in one requirement. Each layer has its own page with the code and the reasoning.
The requirement
A hospital network receives patient referrals from clinics and routes each one to a regional coordination team.
As a referral coordinator, I want every referral for my region to appear on my board the moment it is routed, with the urgent ones announced, so that I triage the right patients first and never see another region’s patients.
Acceptance criteria:
- A referral with a Region is assigned to the least-loaded active coordinator of that region. Changing the region reassigns it. Reopening a closed referral reassigns it too.
- Referrals are private by default. Only the owner, the regional coordinator group and the assigned coordinator can open one, and nobody sees a region that is not theirs.
- When the region changes, the previous region’s access is revoked in the same transaction. When a referral closes, the regional group drops from Edit to Read.
- A referral cannot be declined without a reason, and cannot leave
Newwithout a region. - When a referral becomes Urgent, the assigned coordinator gets a bell notification and an email.
- The coordinator’s board (an LWC) lists their open referrals, shows which ones they can edit, and lets them mark a referral as triaged without a page reload.
- All of it survives a bulk load of 200 referrals in one transaction, and a nightly job can recalculate access if group membership or routing configuration changed.
Data model
Referral__c carries Region__c, Status__c (New, Triaged, Scheduled, Completed, Declined), Priority__c (Urgent, Routine) and Routed_On__c.
Architecture
The batch class that recalculates access (see Record access) calls the same SharingService, so there is one definition of who should see what.
Who can do what
Access is configuration, so it ships as Permission Sets and not as profile edits.
| Persona | Permission set | Gets |
|---|---|---|
| Intake agent | Referral_Intake |
Create and edit Referral__c they own, read Contact and Account |
| Coordinator | Referral_Coordinator |
Read and edit Referral__c shared with them, create Referral_Note__c, the Urgent_Referral notification type |
| Admin | none needed | Manages the groups, the routing metadata and the Flow |
The pages
- Triggers: one trigger per object, a handler framework, change detection, a recursion guard, and the routing rule.
- SOQL: selector classes, relationship and aggregate queries,
WITH USER_MODE, and the SOQL-in-a-loop mistake. - LWC wrappers: the DTO classes, a cacheable method, error handling and the LWC that uses them.
- Record access: Apex managed sharing, the
__Shareobject, revoking stale access and the recalculation batch. - Flows and notifications: the record-triggered Flow, the same behaviour in Apex, and when to pick which.
- Tests: a data factory, a 200-record test and access assertions with
System.runAs.