DML Statements and Operations – Data Manipulation Language (DML) – Salesforce Certified Platform Developer I Study Guide

5.1  DML Statements and Operations

DML refers to the set of statements and operations that you can use to interact with records in the Salesforce database. DML operations are used to create, retrieve, update, and delete records in various Salesforce objects, such as custom and standard objects like accounts, contacts, opportunities, and more.

Salesforce provides a set of DML statements that you can use in your Apex code to perform these operations. You can execute these code examples one by one in the Anonymous Apex window. After running each example, you can check the results by opening the Account list in your Salesforce Sales Cloud and refreshing your browser.

Please note that executing DML operations in a production environment should be done with caution, especially when performing deletions. It’s recommended to test such operations in a sandbox environment first to ensure the desired results.

Insert: Used to create new records in the database.

// Example List of Strings

List<String> fruitNames = new List<String>{‘Apple’, ‘Banana’, ‘Orange’, ‘Grapes’};

  • Create a list to store Account records List<Account> fruitAccountsList = new List<Account>();
  • Populate the list with Account records

for (String fruit : fruitNames) {

Account newFruitAccount = new Account(Name=fruit); fruitAccountsList.add(newFruitAccount);

}

  • Insert the Account records into the database insert fruitAccountsList;
  • Query the inserted records to verify

List<Account> insertedAccounts = [SELECT Id, Name FROM Account WHERE Name IN :fruitNames];

  • Iterate over the inserted records to verify for (Account fruitAccount : insertedAccounts) {

System.debug(‘Inserted Fruit Account: ‘ + + fruitAccount.Id + ‘ – ‘ + fruitAccount.Name);

}

In this example, we create a list (fruitAccountsList) to store Account records. We populate this list in a loop by creating Account instances for each fruit. Then, the insert statement is used to insert all the records in the fruitAccountsList into the Salesforce database. This includes generating a new unique ID for each Account object created in your loop. The Salesforce platform handles the generation of these IDs internally, and you don’t need to explicitly manage or sign them yourself.

Upsert: Used to either update existing records or insert new records based on matching criteria.

// Example List of Strings

List<String> fruitNames = new List<String>{‘Apple’, ‘Banana’, ‘Orange’, ‘Grapes’};

  • Create a list to store Account records List<Account> fruitAccountsList = new List<Account>();
  • Populate the list with Account records

for (String fruit : fruitNames) {

Account newFruitAccount = new Account(Name=fruit); fruitAccountsList.add(newFruitAccount);

}

  • Upsert the Account records using Database.upsert Database.upsert(fruitAccountsList, false);
  • Upsert the Account records

List<Account> upsertedAccounts = [SELECT Id, Name FROM Account WHERE Name IN :fruitNames];

// Iterate over the upserted records to verify

for (Account fruitAccount : fruitAccountsList) {

System.debug(‘Upserted Fruit Account: ‘ + fruitAccount);

}

Salesforce will automatically use the Id field to match records. Since we’re creating new Account records without specifying Ids, the upsert operation will effectively perform an insert for all records. If any of the records had existing Ids that matched records in the database, those records would be updated instead of inserted.