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 - fruitsand 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 - addmethod.
- Iteration: We iterate through the list using a - for-eachloop, 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 - forloop 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 - Accountobject. The query specifies that we want the- Idand- Namefields.
- Query Execution: The query is executed, and the results are stored in a - List<Account>named- accounts.
- Iteration Through Results: We use a - for-eachloop to iterate through the queried- Accountrecords, 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 - Accountobject named- newAccountwith the name 'New Account'. We then use the- insertstatement to add this new account to the Salesforce database.
- Update Operation: We modify the - Namefield of the- newAccountobject to 'Updated Account' and use the- updatestatement 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.

