ApexSensei

Drills › Write Apex

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: 10 and Null hot: false
  • score 75, Lead open → Open boosted: 85 and Open hot: true
  • score 75, Lead closed → Closed hot: false

Requirements:

  1. Give a null leadScore a default of 0 before you add the bonus. Use ? : or ??.
  2. Add 10 bonus points to the safe score.
  3. The Lead is hot when the boosted score is 80 or more and the Lead is open. Join the two tests with &&.
  4. Print Null boosted: and Null hot: for case A, Open boosted: and Open hot: for case B, and Closed 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.

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Money is a Decimal · Clean a Contact name ›