Salesforce DML and Database DML operations

                         DML & Database DML Operations

1. Standard DML Operations (insert, update, delete, undelete):

These are the basic operations in Salesforce used to create, modify, or delete records. These operations are used in Apex to perform actions on Salesforce records.

insert:     Used to insert new records into the database.

// Insert a new Account record using DML 

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

insert acc; // Using DML insert


update:   Used to modify existing records in the database.

// Update an existing Account record using DML

Account acc = [SELECT Id, Name FROM Account WHERE Name = 'New Account' LIMIT 1];

acc.Name = 'Updated Account Name';

update acc; // Using DML update


delete:     Used to delete records from the database.

// Delete an Account record using DML

Account acc = [SELECT Id FROM Account WHERE Name = 'Updated Account Name' LIMIT 1];

delete acc; // Using DML delete


undelete: Used to restore records that were deleted (Soft deleted).

// Undelete an Account record using DML

Account acc = [SELECT Id FROM Account WHERE Name = 'Updated Account Name' ALL ROWS LIMIT 1];

undelete acc; // Using DML undelete

=>Direct operations like insert, update, delete, undelete. These do not allow partial success and do not provide as much control over errors.


2. Database DML Operations (Database.insert(), Database.update(), Database.delete(), Database.undelete()):

These are the same as the standard DML operations but provide more control and fine-grained error handling, especially in bulk operations.

Database.insert(): 

     Inserts new records into the database. It allows partial success when inserting multiple records.

Description: Inserts records with error handling options, allowing partial success or failure.

eg:

Account acc = new Account(Name = 'New Account with Database.insert');

Database.SaveResult result = Database.insert(acc, false); // false means not allowing partial success

if (result.isSuccess()) {

    System.debug('Record inserted successfully');

} else {

    System.debug('Insert failed: ' + result.getErrors());

}

Database.update(): 

       Updates existing records in the database. Like insert(), it also supports partial success.

Description: Updates records with error handling options, allowing partial success or failure.

eg:

Account acc = [SELECT Id, Name FROM Account WHERE Name = 'New Account with Database.insert' LIMIT 1];

acc.Name = 'Updated Name with Database.update';

Database.SaveResult result = Database.update(acc, false); // false means not allowing partial success

if (result.isSuccess()) {

    System.debug('Record updated successfully');

} else {

    System.debug('Update failed: ' + result.getErrors());

}

Database.delete(): 

       Deletes records from the database. Allows partial success when deleting multiple records.

Description: Deletes records with error handling options, allowing partial success or failure.

eg:

Account acc = [SELECT Id FROM Account WHERE Name = 'Updated Name with Database.update' LIMIT 1];

Database.SaveResult result = Database.delete(acc, false); // false means not allowing partial success

if (result.isSuccess()) {

    System.debug('Record deleted successfully');

} else {

    System.debug('Delete failed: ' + result.getErrors());

}

Database.undelete(): 

       Undeletes (restores) deleted records. It also supports partial success.

Description: Undeletes records with error handling options, allowing partial success or failure.

eg:

Account acc = [SELECT Id FROM Account WHERE Name = 'Updated Name with Database.update' ALL ROWS LIMIT 1];

Database.SaveResult result = Database.undelete(acc, false); // false means not allowing partial success

if (result.isSuccess()) {

    System.debug('Record undeleted successfully');

} else {

    System.debug('Undelete failed: ' + result.getErrors());

}

=>Methods like Database.insert(), Database.update(), etc., allow partial success and provide Database.SaveResult for tracking the status of each record.


3. Database.SaveResult:

This is not a DML operation itself, but a result object returned when using the Database.insert(), Database.update(), Database.delete(), or Database.undelete() methods.

  • It helps to track the success or failure of each record in bulk operations.
  • Provides detailed information about the success or failure of each record using isSuccess() and getErrors().
  • If you perform a bulk insert, update, or delete, you get a
    Database.SaveResult[] array that contains the result of each operation.


Differences Between Standard DML and Database DML

Comments

Popular posts from this blog

🌐 Real-Time Salesforce to Oracle REST API Integration – Beginner Guide with JSON and Logging

Oracle to Salesforce Integration: Handling Inbound REST API

Salesforce Integration Basic Details