Use data manipulation language (DML) operations to insert, update,
delete, and restore data in a database.
You can execute DML operations using two different forms:
- Apex DML statements, such as:
- Apex DML database methods, such as:
Database.SaveResult[] result = Database.Insert(SObject[])
While most DML operations are available in either form, some exist
only in one form or the other.
The different DML operation forms enable different types of exception
processing:
- 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 DML database 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 DML database methods also include a
syntax that supports thrown exceptions, similar to DML statements.
The following Apex DML operations are available:
System Context and
Sharing Rules
Most DML operations execute in system
context, ignoring the current user's permissions, field-level security,
organization-wide defaults, position in the role hierarchy, and sharing
rules. However, when a DML operation is called in a class defined
with the with sharing keywords,
the current user's sharing rules are taken into account. For more
information, see Using the with sharing or without
sharing Keywords.
String Field Truncation
and API Version
Apex classes and triggers saved (compiled) using API version
15.0 and higher produce a runtime error if you assign a String value
that is too long for the field.