What is DML Operations in Salesforce Apex

In our previous blog post we had discussed about What is SOSL Queries in Salesforce Apex.In these blog post we discuss about What is DML Operations in Salesforce Apex

What is DML Operations in Salesforce Apex

What Are DML Operations

DML stands for Data Manipulation Language. It refers to the operations used to manipulate data in the Salesforce database. These operations allow developers to insert, update, delete, or restore records. DML operations ensure that records are managed effectively in the Salesforce database without needing complex SQL queries.

In Salesforce, data integrity is key. DML operations are fundamental to achieving this, as they allow developers to interact with the database and maintain accurate, real-time data.

Types of DML Operations in Salesforce Apex

Salesforce provides five core DML operations that developers can use within Apex to interact with the database:

  • Insert: Adds new records.
  • Update: Modifies existing records.
  • Upsert: Updates or inserts records based on whether they exist or not.
  • Delete: Removes records from the database.
  • Undelete: Restores records that have been deleted.

Let’s explore each of these operations in detail.

Insert Operation: Adding New Records

The Insert operation allows developers to add new records to the Salesforce database. It’s the simplest form of DML operation, used when you need to create a new record, whether it’s an Account, Contact, or any other custom object.

Syntax:

Account acc = new Account(Name = 'New Account');


insert acc;

In this example, a new Account record is created with the name “New Account” and then inserted into the database using the insert keyword.

Update Operation: Modifying Existing Records

The Update operation is used to modify existing records. When you need to change details on a record that already exists in the database, the update operation ensures these changes are reflected.

Syntax:

Account acc = [SELECT Id, Name FROM Account WHERE Name = 'Existing Account'];
acc.Name = 'Updated Account Name';
update acc;

In this example, we first retrieve an existing Account record and then modify its name. The update keyword commits the changes to the database.

Upsert Operation: Insert or Update Records

The Upsert operation is unique because it combines the functionality of both Insert and Update. If the record exists, it updates the record; if it doesn’t, it creates a new one.

Syntax:

Account acc = new Account(Name = 'Upserted Account', External_Id__c = '123');
upsert acc External_Id__c;

In this case, if an account with External_Id__c already exists, it will be updated. If not, a new record will be created.

Delete Operation: Removing Records

The Delete operation removes one or more records from the Salesforce database. This operation is irreversible, so it’s essential to use it cautiously.

Syntax:

Account acc = [SELECT Id FROM Account WHERE Name = 'Old Account'];
delete acc;

This example deletes an existing Account record based on the provided criteria.

Undelete Operation: Restoring Deleted Records

The Undelete operation is Salesforce’s way of “recycling” deleted records. It allows developers to restore records that were previously deleted.

Syntax:

Account acc = [SELECT Id FROM Account WHERE IsDeleted = TRUE LIMIT 1 ALL ROWS];
undelete acc;

This query finds deleted records and restores them using the undelete keyword.

Bulk DML Operations

When working with large datasets, DML operations can be performed in bulk. This is especially useful when you need to handle multiple records at once. For example, instead of inserting one Account at a time, you can insert a list of Accounts in a single transaction.

Example:

List<Account> accounts = new List<Account>{
new Account(Name = 'Account 1'),
new Account(Name = 'Account 2')
};
insert accounts;

Handling DML Exceptions

Like any database interaction, DML operations can occasionally fail. To handle such failures gracefully, Salesforce provides Try-Catch blocks to catch exceptions.

Example:

try {
insert accounts;
} catch (DmlException e) {
System.debug('An error occurred: ' + e.getMessage());
}

DML Statements vs. Database Methods

Salesforce offers two ways to perform DML operations: using DML Statements (like insert, update, etc.) or Database Methods (like Database.insert()).

The key difference is that Database Methods allow for partial success. For example, if you’re inserting 10 records and 2 fail, Database Methods can still insert the remaining 8 successfully.

DML Governor Limits in Salesforce

Salesforce enforces Governor Limits to ensure the platform’s stability. These limits restrict the number of DML operations you can perform in a single transaction. For example, a single transaction can only have a maximum of 150 DML statements.

Impact of DML Operations on Triggers

Whenever a DML operation occurs, it can invoke triggers. Triggers allow developers to perform actions before or after a DML operation on a record, such as validating data or automating tasks.

Working with DML Operations in Batch Apex

In cases where a large number of records need processing, Batch Apex can be utilized. DML operations are commonly used within these batch processes to handle bulk data efficiently.

Conclusion

Understanding DML operations in Salesforce Apex is crucial for developers working on the platform. These operations enable you to interact with the Salesforce database, ensuring that records are created, updated, or deleted as required. By mastering DML operations, you’ll be able to build more efficient, reliable applications that manage data effectively.

We Want to more About What is DML Operations in Salesforce Apex Click Here

FAQs

What happens if a DML operation fails?

If a DML operation fails, Salesforce will throw a DmlException, which can be handled using Try-Catch blocks.

Can we undo a delete operation in Salesforce?

Yes, deleted records can be restored using the Undelete operation within a certain retention period.

What is the difference between DML statements and Database methods?

DML statements are simpler but fail entirely if any record encounters an issue, while Database methods allow partial success.

How can we optimize DML operations for bulk data?

By using bulk DML operations and handling exceptions appropriately, you can process large datasets efficiently.

What are Salesforce’s Governor Limits on DML operations?

Salesforce enforces a limit of 150 DML statements per transaction, among other restrictions, to ensure platform performance

In our next blog post we will discuss about Exceptional Handling in Salesforce Apex

Spread the love

2 thoughts on “What is DML Operations in Salesforce Apex

Leave a Reply

Your email address will not be published. Required fields are marked *