Notes
Contains methods for creating and manipulating data.
Some Database methods also exist as DML statements.
Database Methods
These Database methods are static and are called on the class name.
Insert Method:
Database.insert()
Adds an sObject, such as an individual account or contact, to your organization's data.
Inserting single Account:
Account a = new Account(name = 'Acme1');
Database.insert(a,false);
Inserting a List of Accounts:
Account ac1 = new Account(name = 'Acme1');
Account ac2 = new Account(name = 'Acme1');
List<Account> acList = new List<Account>();
acList.add(ac1);
acList.add(ac2);
Database.SaveResult[] results = Database.insert(acList,false);
System.debug(listSaveResult);
Note: By default, the allOrNone parameter is true, which means that the Database method behaves like its DML statement counterpart and will throw an exception if a failure is encountered.
These statements are equivalent:
Database.insert(recordList);
Database.insert(recordList, true);
Insert Records with Partial Success
Create a list of Contacts:
List<Contact> conList = new List<Contact>();
Contact con1 = new Contact(FirstName='Joe',LastName='Smith',Department='Finance');
Contact con2 = new Contact(FirstName='Kathy',LastName='Smith',Department='Technology');
Contact con3 = new Contact(FirstName='Caroline',LastName='Roth',Department='Finance');
Contact con4 = new Contact();
conList.add(con1);
conList.add(con2);
conList.add(con3);
conList.add(con4);
Bulk insert all contacts with one DML call:
Database.SaveResult[] srList = Database.insert(conList, false);
Note: con1, con2 and con3 will be inserted but con4 will not be inserted.
Iterate through each returned result:
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
System.debug('Successfully inserted contact. Contact ID: ' + sr.getId());
} else {
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Contact fields that affected this error: ' + err.getFields());
}
}
}
Use getId()
method to get the ID of the record that was successfully processed (Inserted):
Use isSuccess()
method to get information about successfully inserted records:
User getErrors()
method to get the errors if the process was unsuccessful:
getSobjectType()
:
Account ac = new Account();
Database.insert(ac, False);
System.debug(ac.getSobjectType());
Complete Code
List<Contact> conList = new List<Contact>();
Contact con1 = new Contact(FirstName='Joe',LastName='Smith',Department='Finance');
Contact con2 = new Contact(FirstName='Kathy',LastName='Smith',Department='Technology');
Contact con3 = new Contact(FirstName='Caroline',LastName='Roth',Department='Finance');
Contact con4 = new Contact();
conList.add(con1);
conList.add(con2);
conList.add(con3);
conList.add(con4);
Database.SaveResult[] srList = Database.insert(conList, false);
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
System.debug('Successfully inserted contact. Contact ID: ' + sr.getId());
} else {
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Contact fields that affected this error: ' + err.getFields());
}
}
}