What Is A List?
Salesforce and roughly every other language’s documentation define a list as “an ordered collection of elements that are distinguishable by their indices.”
Right. Thanks. Very clear. If you didn’t already know what a list was, that sentence has done nothing for you.
Here’s the human version. A list is a numbered line of things. Each item sits in a position, positions start at zero, and you can grab any item by asking for its number.
List<Integer> numbers = new List<Integer>{ 12, 45, 7 };
// position: 0 1 2
// value: 12 45 7Two things define a list, and they’re what separate it from the other two collection types:
1. It’s ordered. Items stay in the order you put them, and each has a fixed position.
2. It allows duplicates. Add the number 12 four times and you’ll have four 12s.
Zero. Not One. Zero.
Indices start at 0. Always. The first item is at position 0, the second at 1, and so on.
Every developer alive has at some point confidently asked for position 1 expecting the first item and got the second one instead. Then spent a while feeling betrayed by the universe. Consider yourself warned.
List<String> tacos = new List<String>{ 'Crunchy', 'Soft', 'Doritos Locos' };
System.debug(tacos.get(0)); //Crunchy
System.debug(tacos[0]); //Crunchy - same thing, shorter
System.debug(tacos.get(1)); //SoftBoth forms work. The square bracket version is less typing and reads fine, so most people use it.
Ask for a position that doesn’t exist and you get List index out of bounds, which is Apex’s way of saying “there is nothing there, what are you doing.”
The Methods You’ll Use Constantly
List<String> tacos = new List<String>();
tacos.add('Crunchy'); //stick one on the end
tacos.add('Soft');
tacos.add(0, 'Breakfast'); //insert at a specific position
tacos.size(); //how many? 3
tacos.isEmpty(); //anything in here? false
tacos.contains('Soft'); //is this in here? true
tacos.remove(0); //bin the item at position 0
tacos.set(0, 'Hard Shell'); //overwrite position 0
tacos.clear(); //empty the whole thingUse isEmpty() rather than size() == 0. Same result, reads better, and it’s what everyone else does.
Full list of what’s available is in the List class documentation.
Where You’ll Actually Meet Lists
Here’s the thing that makes lists the collection you’ll use most: every SOQL query returns one.
List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 10];Query the database, get a list back. Then hand that list straight to a DML statement and update every record in one shot:
List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 10];
for (Account acct : accounts)
{
acct.Name = acct.Name + ' - Updated';
}
update accounts; //ONE DML statement for all tenThat’s the bulkification pattern from last episode, in real code. Gather everything into a list, hit the database once.
Don’t worry about the for loop syntax yet, that’s episode 29.
One Trap Worth Knowing
Assigning a SOQL query straight to a single record instead of a list looks tidy and will eventually explode:
//Fine... until the query returns nothing.
Account acct = [SELECT Id, Name FROM Account WHERE Name = 'Kevin' LIMIT 1];If no Account is called Kevin, you get List has no rows for assignment to SObject. Which is a beautifully unhelpful error, given you never mentioned a list.
Query into a list and check it first:
List<Account> accts = [SELECT Id, Name FROM Account WHERE Name = 'Kevin' LIMIT 1];
if (!accts.isEmpty())
{
String acctName = accts[0].Name;
}Or use the safe navigation operator and do the whole thing in one line. Your call.
What’s Next
Lists: ordered, duplicates allowed, zero indexed, and what every SOQL query hands you.
In the next episode we look at Sets, which are lists with the two defining features turned off. Sounds worse. Isn’t.
See you next time!
Get Coding With The Force Merch!!
We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.
Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!
Check Out More Coding With The Force Stuff!
If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!
Youtube
Patreon
Github
Facebook
Twitter
Instagram
Salesforce Development Books I Recommend
Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps
Good Non-SF Specific Development Books:
Clean Code
Clean Architecture