Handle Edge Cases
By the end of this lesson, you'll be able to:
- Handle an Account with zero Cases, zero Contacts, or no won Opportunities
- Handle an invalid or null accountId gracefully
- Write tests confirming each edge case behaves correctly, not just the happy path
Prerequisites: "Write Comprehensive Tests"
The zero-data Account
@isTest
static void newAccountWithNoDataReturnsZeroedSummary() {
Account acc = new Account(Name = 'Brand New Account');
insert acc;
AccountHealthService service = new AccountHealthService();
AccountHealthSummary summary = service.getHealthSummary(acc.Id);
System.assertEquals(0, summary.openCaseCount);
System.assertEquals(0, summary.averageResolutionDays);
System.assertEquals(0, summary.totalWonRevenue);
}
@isTest— marks the test method.static void newAccountWithNoDataReturnsZeroedSummary() {— the name states the scenario under test.Account acc = new Account(Name = 'Brand New Account');— deliberately no related Cases, Contacts, or revenue.insert acc;— saves it, with nothing else inserted.- (blank line)
AccountHealthService service = new AccountHealthService();— the class under test.AccountHealthSummary summary = service.getHealthSummary(acc.Id);— calls the combining method against an Account with zero related data.- (blank line)
System.assertEquals(0, summary.openCaseCount);— confirms the zero-Case case.System.assertEquals(0, summary.averageResolutionDays);— confirms the zero-closed-Case case.System.assertEquals(0, summary.totalWonRevenue);— confirms the blank-revenue-field case.}— closes the test method.
Every method built in Lesson 2 already handles empty results gracefully (if (closedCases.isEmpty()) { return 0; }) — this test formally confirms that design decision, exactly Module 17's habit of testing the empty/zero case explicitly rather than assuming it "probably works."
A null or invalid accountId
@AuraEnabled(cacheable=true)
public static AccountHealthSummary getAccountHealth(Id accountId) {
if (accountId == null) {
AuraHandledException ex = new AuraHandledException('An account must be selected to view health data.');
ex.setMessage('An account must be selected to view health data.');
throw ex;
}
return new AccountHealthService().getHealthSummary(accountId);
}
@AuraEnabled(cacheable=true)— unchanged from Lesson 4.public static AccountHealthSummary getAccountHealth(Id accountId) {— the same controller method.if (accountId == null) {— checked before calling the service at all.AuraHandledException ex = new AuraHandledException('An account must be selected to view health data.');— Module 34's exception type, constructed with a UI-safe message.ex.setMessage('An account must be selected to view health data.');— see the warning below.throw ex;— throws it, ending the method here.}— closes theif.return new AccountHealthService().getHealthSummary(accountId);— unchanged, only reached with a non-nullaccountId.}— closes the method.
Warning: Line 5's setMessage() call is not redundant with line 4's constructor argument. AuraHandledException has a genuine Salesforce quirk: the message passed to its constructor is not what getMessage() returns to a catching test or the LWC's error handler — only an explicit setMessage() call reliably sets it. Skip line 5, and the exception still throws, but the carefully written message may not actually reach the user.
This is Module 34's AuraHandledException lesson, applied to a genuinely real failure mode: an LWC could conceivably call this before a user has actually selected an Account — a clear, UI-safe message here is far better than whatever cryptic error a null Id would eventually cause deeper in the query logic.
Exercise
Write a test confirming getAccountHealth throws an AuraHandledException when called with a null accountId.
Show hint
Use the try/System.assert(false)/catch pattern from Module 31.
Handle Edge Cases Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A brand-new Account with no Cases, no Contacts, and no closed deals is a completely normal, expected scenario for this dashboard — this lesson makes sure it displays cleanly rather than breaking.