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

Merge: Is only available for certain standard objects in Salesforce, such as Account, Contact, Lead, and Case, and you can merge up to three records at a time.

Account masterAccount = new Account(Name = ‘MyName’); insert masterAccount;

Account duplicateAccount = new Account(Name = ‘MyName’); insert duplicateAccount;

Database.merge(masterAccount, duplicateAccount);

  • Perform the merge operation
  • Query the master account to get its updated state

masterAccount = [SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account WHERE Id = :masterAccount.Id];

  • Verify the result of the merge operation System.debug(‘Merged Account Name: ‘ + masterAccount.Name);

It creates an Account record named “MyName” and inserts it into the database. This account will serve as the master account during the merge operation. It creates another Account record with the same name (“MyName”) and inserts it into the database. This account is a duplicate that will be merged into the master account.

These DML operations are crucial for interacting with the Salesforce database and performing data-related tasks in your Apex code. It’s important to note that DML operations can affect a single record or multiple records using collections like lists or maps, and they ensure proper data integrity and security within the Salesforce platform. Additionally, DML operations might trigger various database events, such triggers and flows, which can execute additional logic based on the data changes.

The governor limit in Salesforce is 150 statements in a transaction, and this limit applies to the total number of DML statements executed in a transaction, regardless of the type of operation or the number of records affected by each statement. For example, if you perform 100 insert statements and 50 update statements, you would have reached the limit of 150 DML statements.

The limit for the number of records that can be processed by Apex DML operations in a single transaction is 10,000. This means to perform insert, update, or delete operations not more than 10,000 records within a single transaction.

You can use Limits.getDMLRows() method to implement logic that checks the number of records affected by DML operations and displays a warning message to the user if the limit is approaching. This can help users avoid hitting the governor limits and take necessary actions to optimize their code.

Here’s an example of how you can use Limits.getDMLRows() method to check the number of records affected by DML operations:

List<Account> accountsToInsert = new List<Account>();

  • Adding sample Account records to the list accountsToInsert.add(new Account(Name=’Account1′)); accountsToInsert.add(new Account(Name=’Account2′)); accountsToInsert.add(new Account(Name=’Account3′));
  • Perform DML operation (insert)

insert accountsToInsert;

  • Check the number of records affected by DML operation Integer dmlRows = Limits.getDMLRows();

System.debug(‘Number of records affected by DML operation: ‘ + dmlRows);

In the above example, the insert statement inserts the accountsToInsert list, and then Limits.getDMLRows() method is used to retrieve the number of records affected by the insert operation. If the number 6 or more is being returned by Limits.getDMLRows() after inserting the three sample records, it’s because Salesforce counts the records for certain DML operations. For example, when you insert a record that has a master-detail relationship, Salesforce might count not only the parent record you explicitly inserted but also any child records created due to the relationship. In the case of Account records, if you have any child records related through master-detail relationships, they might be counted as well. Make sure that there are no triggers, processes, or other automation that create additional records during the insert operation.

By using Limits.getDMLRows() method, you can have better control over the usage of DML governor limits and optimize your code accordingly.

It’s important to note that Limits.getDMLRows() method only returns the number of records affected by DML operations in the current transaction.

Avoiding DML operations inside loops is a best practice in Salesforce Apex to ensure efficient and optimized code execution. Performing DML operations, such as inserts, updates, or deletes, within a loop can lead to performance issues, governor limits breaches, and slow execution times. Instead, you should accumulate the necessary data and perform the DML operation outside the loop. Here’s an example to illustrate this best practice:

  • Define a set to store unique AccountIds Set<Id> uniqueAccountIds = new Set<Id>();
  • Define a list to accumulate data for DML operation List<Account> accountsToUpdate = new List<Account>();
  • Assume you have a loop to retrieve and process data

for (Contact contact : [SELECT Id, AccountId FROM Contact WHERE AccountId != null LIMIT 10]) {

  • Check if the AccountId is unique before adding to the list if (!uniqueAccountIds.contains(contact.AccountId)) {

// Perform some processing on the data

Account acc = new Account(Id = contact.AccountId, Name = ‘Updated Name’);

  • Add the modified record to the list instead of performing
  • DML inside the loop

accountsToUpdate.add(acc);

  • Add the AccountId to the set to track uniqueness uniqueAccountIds.add(contact.AccountId);

}

}

  • Perform the DML operation outside the loop update accountsToUpdate;
  • Debug statement to verify

System.debug(‘Updated Accounts: ‘ + accountsToUpdate);

In this example, we use a loop to retrieve Contacts and process them. Instead of updating the Account records within the loop, we accumulate them in a list (accountsToUpdate). After the loop, we perform a single update DML operation on the list outside the loop, which is a more efficient and governor limits-friendly approach. The System.debug statement is used to verify the updated accounts in the debug logs.