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

Update: Used to modify existing 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 = [SELECT Id, Name FROM Account WHERE Name IN :fruitNames];

  • Update all account names in the database for (Account account : fruitAccountsList) { account.Name = ‘Updated’ + account.Name;
  • Prepend ‘Updated ‘ to each name

}

if (!fruitAccountsList.isEmpty()) {

update fruitAccountsList;

// Iterate over the updated records to verify

for (Account updatedFruitAccount : fruitAccountsList) {

System.debug(‘Updated Fruit Account: ‘ + updatedFruitAccount.Name);

}

} else {

System.debug(‘No matching accounts found to update.’);

}

In this code, each Account name is updated by prepending ‘Updated’ to its existing name. These modified names are then used to update the existing Account records in the database. The debug statement will show the updated names of the Account records in the debug logs. If no matching accounts are found, a debug message will indicate this.

Delete: Used to remove records from the database.

// Example List of Strings

List<String> fruitAccountsList = new List<String>{‘Updated Apple’};

  • Query the Account records with the updated alternative names List<Account> accountsToDelete = [SELECT Id FROM Account WHERE Name IN :fruitAccountsList];
  • Delete the records from the database

delete accountsToDelete;

  • Show that the records are deleted String deletedAccountIds = ‘ ‘;

for (Account deletedAccount : accountsToDelete) { deletedAccountIds += deletedAccount.Id + ‘, ‘;

}

System.debug(‘Deleted Fruit Account IDs: ‘ + deletedAccountIds);

This code queries the Account records with the updated names and then uses the delete DML operation to delete those records. The debug statement shows the deleted records in the debug logs.

Undelete: Used to restore records that have been previously deleted from the database. This operation is specifically used to recover records that were soft-deleted and placed in the recycle bin rather than permanently removed.

When records are deleted in Salesforce, they are initially moved to the recycle bin where they can be restored or permanently deleted. The undelete operation allows you to retrieve records from there and bring them back into the active database. Here’s an example of how undelete can be used in Salesforce Apex:

  • Example List of Strings List<String> fruitAccountsList = new List<String>{‘Updated Apple’};
  • Query the deleted records from the Recycle Bin List<Account> deletedAccounts = [SELECT Id FROM Account WHERE Name IN : fruitAccountsList ALL ROWS];
  • Undelete the records

Database.undelete(deletedAccounts);

// Query the undeleted records to get their details

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

// Debug the undeleted accounts

for (Account acc : undeletedAccounts) {

System.debug(‘Undeleted Account: ‘ + acc.Name);

}

This code queries the deleted Account records with the updated alternative names from the Salesforce Recycle Bin using the ALL ROWS clause and then uses the Database. undelete method to undelete them. The debug statement shows the undeleted records in the debug logs.