Return a value and record work
Think in classes · Beginner · 12 min · Runs in your Salesforce Developer org · Free
Scenario. A sales queue needs to suggest work and track actions without confusing returned data with side effects.
Contrast a method that returns a Lead action with a void method that changes state.
What you will learn
A method's return type promises a value back to the caller. A void method returns nothing. It usually does an action or changes the object's fields.
String nextStep() {
return 'Review';
}
void markDone() {
doneCount++;
}
Gotcha. Every path through a non-void method must return a value of its declared type.
The task
Finish a Lead work queue that returns a next action and uses a void method to count finished work.
Examples:
nextAction('Maya Chen')→Action: Review Maya Chen- two calls to
markProcessed()→Processed: 2 - a brand-new queue →
New queue: 0
Requirements:
- Make
nextActionreturnReviewplus the Lead name. - Make the
voidmethodmarkProcessedadd 1 to the private count. - Call
markProcessed()twice on one queue, then read the count withgetProcessedCount(). - Create a second queue to show that its count starts at 0.
- Print
Action:,Processed:andNew queue:, each followed by its value.
Starter code
class LeadWorkQueue {
private Integer processedCount = 0;
public String nextAction(String leadName) {
return '';
}
public void markProcessed() {
}
public Integer getProcessedCount() {
return processedCount;
}
}
LeadWorkQueue queue = new LeadWorkQueue();
When it passes
The String method returns an action, two void calls raise one queue's count to 2, and a new queue still reads 0.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.
‹ Build queue labels with parameters · Initialize an appointment plan ›