Apex Basics & Database : Manipulating Records with DML

Note: All credit goes to Salesforce, I am just a developer who enjoy playing trailhead and try to make note to myself.

Manipulating Records with DML

DML statements that are available:

  • insert
  • update
  • upsert-creates new records and updates sObject records within a single statement, using a specified field to determine the presence of existing objects, or the ID field if no field is specified.
  • delete
  • undelete
  • merge-merges up to three records of the same sObject type into one of the records, deleting the others, and re-parenting any related records.

Each DML statement accepts either a single sObject or a list (or array) of sObjects. Operating on a list of sObjects is a more efficient way for processing records.

Database Methods

Apex contains the built-in Database class, which provides methods that perform DML operations and mirror the DML statement counterparts.These Database methods are static and are called on the class name.

  • Database.insert()
  • Database.update()
  • Database.upsert()
  • Database.delete()
  • Database.undelete()
  • Database.merge()

Why it different with DML statement?

  • Database methods have an optional allOrNone parameter that allows to specify whether the operation should partially succeed. When this parameter is set to false, if errors occur on a partial set of records, the successful records will be committed and errors will be returned for the failed records. Also, no exceptions are thrown with the partial success option.
  • Database class contains methods that aren’t provided as DML statements. For example, methods used for transaction control and rollback, for emptying the Recycle Bin, and methods related to SOQL queries. 

Should You Use DML Statements or Database Methods?

Use DML statements 
If you want any error that occurs during bulk DML processing to be thrown as an Apex exception that immediately interrupts control flow (by using try. . .catch blocks). This behavior is similar to the way exceptions are handled in most database procedural languages.

Use Database class methods
If you want to allow partial success of a bulk DML operation—if a record fails, the remainder of the DML operation can still succeed. Your application can then inspect the rejected records and possibly retry the operation. When using this form, you can write code that never throws DML exception errors. Instead, your code can use the appropriate results array to judge success or failure. Note that Database methods also include a syntax that supports thrown exceptions, similar to DML statements.

About Transactions

DML operations execute within a transaction. All DML operations in a transaction either complete successfully, or if an error occurs in one operation, the entire transaction is rolled back and no data is committed to the database. The boundary of a transaction can be a trigger, a class method, an anonymous block of code, an Apex page, or a custom Web service method. For example, if a trigger or class creates two accounts and updates one contact, and the contact update fails because of a validation rule failure, the entire transaction rolls back and none of the accounts are persisted in Salesforce.

Code that I wrote for challenge:

public class AccountHandler {
    public static Account insertNewAccount(String accountName){
        Account newAccount =new Account(Name=accountName);
        insert newAccount;
            return newAccount;
        catch(DMLException e){
            return null;


  1. when I tried above program it shows error:

    There was an unexpected error in your org which is preventing this assessment check from completing: System.NullPointerException: Attempt to de-reference a null object


Post a Comment

Popular Posts