While SOQL and SOSL are for Reading data, DML (Data Manipulation Language) is for Modifying it. It allows you to create, update, and remove records in your Salesforce database.
Use insert to bring new records into existence.
Use update or upsert to change existing data.
Use delete and undelete to manage record lifecycles.
You can only perform 150 DML calls in a single transaction. This is why you must Bulkify your code—never put a DML statement inside a loop!
In Apex, you have two ways to modify data. You can either use normal DML statements or use Database methods. The choice depends on how you want to handle errors.
These are "All-or-None" operations. If a single record in a list fails (e.g., due to a validation rule), the entire transaction is rolled back and an exception is thrown.
insert accList;
If you want to allow partial processing in case there are errors, use Database methods. These methods allow valid records to be saved even if others fail.
Database.insert(accList, false);
Database methods add a boolean parameter to control behavior. If set to false, it allows partial processing. This boolean is true by default, meaning it acts like normal DML unless you specify otherwise.
As part of the transaction, a result array is generated that helps you understand success and failures. You can iterate through this array to find exactly which records failed and why.
In Salesforce activities (Tasks and Events), you'll encounter Polymorphic Lookups. These special fields can point to more than one type of object.
The WhoId refers to people. It usually represents a Lead or a Contact. Think of it as the person you are talking to.
// Example: Assigning a Task to a Contact
myTask.WhoId = someContact.Id;
The WhatId refers to non-human objects. It can link to an Account, Opportunity, Case, or any Custom Object.
// Example: Linking an Event to an Account
myEvent.WhatId = someAccount.Id;
| Field Name | Relationship Type | Common Objects |
|---|---|---|
| WhoId | People | Lead, Contact |
| WhatId | Non-Human Objects | Account, Opportunity, Case, Custom Objects |
Salesforce strictly enforces the "People vs. Object" rule. If you put a WhoId instead of a WhatId, or a WhatId instead of a WhoId, you will get a Field Integrity Exception error.
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type
When you use Database methods, the result of a transaction is stored as an array of Database.SaveResult. This is generated when a new or existing record is saved.
This class provides built-in methods to inspect each record's fate:
By holding an array of Database.Error objects, this helps you track and audit what errors were generated as part of this transaction. You can extract the error message and the fields that caused the failure.
// Iterating through results
for (Database.SaveResult sr : srList) {
if (!sr.isSuccess()) {
for(Database.Error err : sr.getErrors()) {
System.debug(err.getMessage());
}
}
}