ApexSensei

Drills › Think in classes

Build a Case summary class

Think in classes · Beginner · 10 min · Runs in your Salesforce Developer org · Free

Scenario. A support tool needs a reusable in-memory summary before any Case record is saved.

Write a small class, create separate Case summary objects, and call a method on each one.

What you will learn

A class groups data (fields) with behavior (methods). Create each object with new, set its fields, then call a method on it.

Ticket item = new Ticket();
item.subject = 'Login';
String text = item.describe();

Each object keeps its own field values.

Gotcha. Declaring a class does not create an object. You still need new.

The task

Create three CaseSummary objects and print the summary each one returns.

Examples:

  • subject Login issue, status New → First: Login issue :: New
  • subject Refund request, status Escalated → Second: Refund request :: Escalated
  • blank subject, status New → Empty subject: (no subject) :: New

Requirements:

  1. Finish describe() so it returns the subject, :: , then the status.
  2. When the subject is blank, use (no subject) in its place.
  3. Create a separate object for each of the three Cases in the examples.
  4. Print First:, Second: and Empty subject:, each followed by that object's describe() result.

Starter code

class CaseSummary {
    public String subject;
    public String status;

    public String describe() {
        return '';
    }
}

CaseSummary first = new CaseSummary();
first.subject = 'Login issue';
first.status = 'New';

When it passes

Three separate objects return their own summaries, and a blank subject shows `(no subject)`.

Try this drill free

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

Build queue labels with parameters ›