Apex basics: Apex practice drills
Variables, numbers, text, dates, if and switch, loops, Lists, Sets and Maps. Your Apex foundation.
Beginner · Free to run
- Store the company name Beginner · 5 min
A String holds text.
- Count open cases Beginner · 5 min
An Integer is a whole number.
- Build the account label Beginner · 5 min
The + operator joins text.
- Mark a high score Beginner · 5 min
An if chooses one path.
- Return the tier you were given Beginner · 5 min
return sends a value back to the caller.
- Comments that survive review Beginner · 5 min
Comments explain code without changing what it does.
- Turn on the VIP flag Beginner · 5 min
A Boolean holds only true or false.
- Set the monthly price Beginner · 5 min
A Decimal holds a number with a decimal point, such as a price.
- Set the renewal date Beginner · 5 min
A Date holds a calendar day.
- Pick the shipping fee Beginner · 5 min
else if adds another test after an if.
- Put two regions in a list Beginner · 5 min
A List<String> holds text values in order.
- Add up the seats Beginner · 5 min
A for-each loop runs once for every item in a list: for (Integer seat : seats) { }.
- Hello, ApexSensei Beginner · 5 min
Apex runs on Salesforce servers, so developers inspect values with System.debug().
- Quick win: double a number Beginner · 5 min
A method takes input and gives back a value with return.
- Quick win: greet by name Beginner · 5 min
+ joins text together.
- Quick win: is it positive? Beginner · 5 min
A comparison like n > 0 is already a Boolean: it is true or false.
- Quick win: make initials Beginner · 5 min
Text has built-in methods.
- Quick win: pick the larger number Beginner · 5 min
if runs a block only when its test is true.
- Quick win: shout it Beginner · 5 min
toUpperCase() returns a new copy of the text in capital letters.
- Quick win: join a full name Beginner · 5 min
When you join two variables you must add the glue yourself.
- Quick win: is the name blank? Beginner · 5 min
Three things look empty to a user: null, '' and ' '.
- Integer division and Math.mod Beginner · 10 min
Use Integer for whole counts.
- Money is a Decimal Beginner · 10 min
Salesforce currency is a Decimal.
- True, false, and null Beginner · 12 min
An empty number in Apex is null, not 0.
- Clean a Contact name Beginner · 12 min
Apex String methods such as trim and capitalize turn messy imported text into clean names.
- Parse an email Beginner · 12 min
indexOf and substring split an email address into the part before and after the @.
- Split and join text Beginner · 12 min
split returns a List<String> and reads its argument as regex, so a real dot is '\\.'.
- Working with dates Beginner · 10 min
Build a Date with Date.newInstance.
- If a Lead is hot Beginner · 12 min
if / else if / else picks one answer from several, in order.
- Switch on Case status Beginner · 10 min
switch on compares one value with a list of when branches and runs the first match.
- For loop: number invoices Beginner · 10 min
A for loop has three parts: a start, a test, and a step.
- For-each over Accounts Beginner · 12 min
A for-each loop walks every item in a List in order, with no index to manage: for (Account acct : accounts) { ...
- While a queue has work Beginner · 12 min
A while loop repeats as long as its test is true.
- Lists of Opportunities Beginner · 15 min
A List<Opportunity> keeps records in order: add puts one on the end, [i] reads position i starting at 0, and size() counts them.
- Maps: Id to Account Beginner · 15 min
A Map<Id, Account> stores each Account under its Id.
- Remove duplicate emails with a Set Beginner · 12 min
A Set<String> keeps each value only once.
- Count Leads by Rating with a Map Beginner · 15 min
A Map<String, Integer> can keep a running count per key.
- Combine conditions with && and || Beginner · 10 min
&& is true only when both sides are true; || is true when either side is.
- Safe navigation with ?. Beginner · 12 min
The ?. operator returns null instead of throwing when the value on its left is null.
- break and continue Beginner · 12 min
continue skips the current loop iteration; break leaves the loop.
- Find the compile error Beginner · 10 min
Apex refuses to run code that will not compile.
- Group records in a Map<String, List<Contact>> Beginner · 12 min
A Map<String, List<Contact>> groups records by a shared field.
- Convert text to Integer, safely Beginner · 12 min
Integer.valueOf(text) returns the number inside text, but throws on null, empty or non-numeric.