Protect a support ticket subject
Think in classes · Beginner · 12 min · Runs in your Salesforce Developer org · Free
Scenario. A support service must prevent callers from bypassing the rules around ticket subject changes.
Keep ticket fields private and change them only through a public method that checks the input.
What you will learn
Access modifiers control who can use a class member. Make a field private when outside code should not change it. Then add public methods that follow your rules.
private String subject;
public String getSubject() {
return subject;
}
Gotcha. private hides a field from other classes, not from the class's own methods. In a script like this one, the script can still reach it too. Real, deployed classes are where the compiler enforces it.
The task
Protect a support ticket so only a public rename method can change its private subject and update count.
Examples:
- a new ticket for
Login issue→Initial: Login issue :: updates 0 rename('Cannot sign in')→Updated: Cannot sign in :: updates 1rename('')changes nothing →Blank rename: Cannot sign in :: updates 1
Requirements:
- Keep
subjectandupdateCountprivate. - In
rename, change the subject and add 1 to the count only when the new subject is not blank (empty, spaces only or null). UseString.isBlank. - Make
summary()return the subject,:: updates, then the count. - Print
Initial:,Updated:andBlank rename:, each followed byticket.summary(). Never setticket.subjectorticket.updateCountfrom outside the class.
Starter code
class SupportTicket {
private String subject;
private Integer updateCount;
public SupportTicket(String subject) {
this.subject = subject;
this.updateCount = 0;
}
public void rename(String newSubject) {
}
public String summary() {
return '';
}
}
SupportTicket ticket = new SupportTicket('Login issue');
When it passes
A valid rename changes the private state once, and a blank rename leaves both private values unchanged.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.
‹ Initialize an appointment plan · Expose a read-only quote total ›