8

Database Class in APEX

What Is Database Class In Apex?

In DML Statements, the entire DML fails with an exception if there is an error or issue with any record. (DML Exception) which you can handle on your own, whereas in database class methods, we can specify whether to allow partial record passing in case of errors.

We can do so by passing an additional boolean parameter.

When this parameter is set as false, and if a record fails, the remainder of the DML operation can still succeed.

public static void main() {
    List < Contact > conList = new List < Contact > ();
    for (Integer i = 0; i < 10; i++) {
        if (i == 5) conList.add(new Contact());
        else conList.add(new Contact(lastname = ’ABC’ + i));
    }
    insert conList; // Throws DML Exception
    Database.insert(conList, false); // Does not throws an exception if a record fails
}

Database.insert(conList);

Database.insert(conList, false);

Database methods in Apex

The following are the major database class methods Salesforce: 

1. Database.insert()

Ex:

List < Account > accountsToInsert = new List < Account > ();
Account accountToUpdate;
for (Integer i = 0; i < 3; i++) {
    Account acc = new Account(Name = 'Simplilearn', BillingCity = 'New York');
    accountsToInsert.add(acc);
}
Database.insert(accountsToInsert);

2. Database.update()

Ex :

Account accountToUpdate = [SELECT BillingCity FROM Account WHERE Name = 'Simplilearn'
    LIMIT 1
];
accountToUpdate.BillingCity = 'San Francisco';
Database.update(accountToUpdate);
Account afterUpdate = [SELECT BillingCity FROM Account WHERE Id = : accountToUpdate.Id];
System.assertEquals(afterUpdate.BillingCity, 'San Francisco');

3. Database.upsert()

Ex:

Database.upsert(sObject / List < sObject > , externalIdField, allOrNone);

Note: If the externalIdField is left blank, then the ‘ Id’ field will be used as the external ID.

List < Account > accountsList = [SELECT Id, Name, BillingCity
    FROM Account WHERE BillingCity = 'San Francisco'
];
for (Account acc: accountsList) {
    acc.BillingCity = 'Las Vegas';
}
Account newAccount = new Account(Name = 'SimpliLearn', BillingCity = 'Palo Alto');
accountsList.add(newAccount);
Database.upsert(accountsList);

4. Database.delete()

Ex:

List < Account > accList = [SELECT Name FROM Account LIMIT 10];
Database.delete(accList, false);

Note:  You cannot delete an account which have cases related to it.

5. Database.undelete()

Ex :

Database.undelete(): 
Ex: List < Account > accList = [SELECT Name FROM Account
    WHERE isDeleted = true ALL ROWS
];
Database.undelete(accList, false);

6. Database.merge()

Syntax :

Database.merge(master_sObject, duplicate(sObject / List < sObject / ID / List < ID > ), allOrNone);

Ex :

List < Account > accList = [SELECT Name FROM Account LIMIT 3];
Account masterRecord = accList[0];
List < Account > dupList = new List < Account > ;
dupList.add(accList[1]);
dupList.add(accList[2]);
Database.merge(masterRecord, dupList);
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Frequently Asked Questions

What is the difference between DML and Database class in Apex?

Standard DML statements throw an exception when a record fails, rolling back the entire transaction. The database class methods allow partial success by accepting a Boolean parameter.

What is Database.insert in Apex?

Database.insert() is a class method used to insert sObject records. Records that pass validations are inserted, while failed records return error details.

What are Database class methods in Salesforce?

The Database class in Apex provides the following key DML methods: Database.insert(), Database.update(), Database.upsert(), Database.delete(), Database.undelete(), and Database.merge().

What is transaction control in Apex?

Transaction control in Apex allows developers to identify savepoints and rollbacks in a single transaction.

Book Free15-Minutes Career Counselling