Design the code structure for a large org where triggers, Flows, LWCs and REST APIs all need the same order-submission logic.
Your confidence:Not rated
Model answer
One service, many thin entry points.
OrderSubmissionService.submit(Set<Id> orderIds)holds the business operation. It's bulk by design and returns a result object with per-record outcomes.- Entry points are thin wrappers:
- Trigger handler โ calls the service for records that changed to Ready.
- Flow โ an
@InvocableMethodthat maps a list of requests to one service call. - LWC โ an
@AuraEnabledcontroller method that validates input and calls the service. - External system โ an
@RestResourceclass that authenticates (Connected App or External Client App), maps JSON to IDs and calls the service.
- Selectors own the queries (consistent fields, user mode). Domain classes hold order-specific rules (validation, state transitions). A Unit of Work commits related changes in order.
- Callouts to ERP are queued (Queueable with a Finalizer for retries) so no entry point waits on ERP.
- Strategy + factory where rules vary (by country or channel), configured in custom metadata.
- Errors: the service returns typed results; wrappers translate them (Flow fault path, LWC toast, REST status code).
- Tests: unit tests for the service with mocked selectors (Stub API), plus a few integration tests per entry point.
โ Key points to hit
โ Red-flag answers
- Copying the logic into each entry point
- Callouts inside the user's transaction
- Invocable methods that process one record at a time
โช Follow-ups
How should the invocable method be written for bulk?
Accept List<Request> and return List<Result>, collect all IDs, call the service once, and map results back in the same order. Flow batches interviews together, so one call may contain many requests.
How do you secure the REST resource?
Expose it only to an integration user through a Connected App or External Client App with the right OAuth flow and scopes, run the service with sharing and user-mode data access, validate input strictly and log calls.