Lists in Salesforce Apex
Lists are a fundamental data structure in Apex, providing a way to store and manipulate collections of elements. Apex supports both lists and arrays, with lists being more versatile and dynamic. Here's an example of how you can declare and use a list in Apex:
// Declare a list of Strings
List<String> fruits = new List<String>{'Apple', 'Orange', 'Banana'};
// Access elements in the list
System.debug(fruits[0]); // Outputs 'Apple'
// Add a new element to the list
fruits.add('Grapes');
// Iterate through the list
for(String fruit : fruits) {
System.debug(fruit);
}
Explanation:
List Declaration: In this example, we declare a list of strings named
fruits
and initialize it with three elements: 'Apple', 'Orange', and 'Banana'.Accessing Elements: We use
System.debug()
to output the first element of the list (fruits[0]
), which is 'Apple'.Adding an Element: We add a new element, 'Grapes', to the list using the
add
method.Iteration: We iterate through the list using a
for-each
loop, printing each element to the debug log.
Loops in Salesforce Apex
Loops are essential for iterating over collections, such as lists or querying records from the database. Apex supports various loop structures, including for
, while
, and do-while
. Let's look at an example using a for
loop:
// Loop through a list of numbers
for(Integer i = 0; i < 5; i++) {
System.debug('Iteration: ' + i);
}
Explanation:
For Loop: This example demonstrates a
for
loop that iterates over a range of numbers from 0 to 4 (i < 5
).Debug Output: Inside the loop, we use
System.debug()
to print the current iteration count (i
) to the debug log.
SOQL (Salesforce Object Query Language)
SOQL is a query language used to retrieve data from Salesforce objects. It resembles SQL but is specifically designed for querying Salesforce data. Here's an example of a simple SOQL query:
// Query all Account records
List<Account> accounts = [SELECT Id, Name FROM Account];
// Iterate through the queried records
for(Account acc : accounts) {
System.debug('Account Name: ' + acc.Name);
}
Explanation:
SOQL Query: We use a SOQL query to retrieve records from the
Account
object. The query specifies that we want theId
andName
fields.Query Execution: The query is executed, and the results are stored in a
List<Account>
namedaccounts
.Iteration Through Results: We use a
for-each
loop to iterate through the queriedAccount
records, and for each record, we print the account name to the debug log.
DML (Data Manipulation Language) Operations
DML operations in Apex allow you to insert, update, delete, and retrieve records in Salesforce. Let's look at examples of inserting and updating records:
// Insert a new Account record
Account newAccount = new Account(Name='New Account');
insert newAccount;
// Update the Account record
newAccount.Name = 'Updated Account';
update newAccount;
Explanation:
Insert Operation: We create a new
Account
object namednewAccount
with the name 'New Account'. We then use theinsert
statement to add this new account to the Salesforce database.Update Operation: We modify the
Name
field of thenewAccount
object to 'Updated Account' and use theupdate
statement to save the changes to the database.
These examples cover only the basics of Salesforce Apex. As you delve deeper into Apex development, you'll encounter more advanced topics like triggers, classes, and custom controllers. However, mastering Lists, Loops, SOQL, and DML operations is crucial for building robust and efficient Salesforce applications.