True, false, and null
Write Apex · Beginner · 12 min · Runs in your Salesforce Developer org · Free
Scenario. Marketing adds 10 points when a Lead attends a webinar. Some Leads have no score yet.
Give a missing Lead score a default before doing math, then decide if the Lead is hot in three cases.
What you will learn
A Boolean is true or false. A missing value is null. Comparing null is safe: null >= 80 is false. Math is not: null + 10 throws. Default a nullable first.
Integer safeScore = score == null ? 0 : score;
Integer sameScore = score ?? 0; // shorter
Booleans combine with && (and), || (or), ! (not). !isClosed means "not closed". Join tests: score >= 80 && !isClosed.
Gotcha. Null is not zero. score == 0 is false when score is null.
The task
Add a 10-point webinar bonus to a Lead score, then decide if the Lead is hot, for three cases.
Examples:
- no score, Lead open →
Null boosted: 10andNull hot: false - score 75, Lead open →
Open boosted: 85andOpen hot: true - score 75, Lead closed →
Closed hot: false
Requirements:
- Give a null
leadScorea default of 0 before you add the bonus. Use? :or??. - Add 10 bonus points to the safe score.
- The Lead is hot when the boosted score is 80 or more and the Lead is open. Join the two tests with
&&. - Print
Null boosted:andNull hot:for case A,Open boosted:andOpen hot:for case B, andClosed hot:for case C.
Starter code
Integer leadScore = null;
Boolean isClosed = false;
// Case A: no score yet, Lead open
// Case B: score 75, Lead open
leadScore = 75;
// Case C: same Lead, now closed
isClosed = true;
When it passes
Case A prints 10 and false, case B prints 85 and true, and case C prints false. Nothing throws a NullPointerException.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.