DML (Data Manipulation Language) is how Apex inserts, updates, deletes, undeletes, and upserts records. Two syntaxes:
Statement form (concise):
apex insert acc; update accList; delete oppList; upsert leads External_Id__c;
Database method form (more options):
apex Database.SaveResult[] results = Database.insert(accList, false); // false = allow partial success
Database methods give you partial-success flag (allOrNone = false) and per-record SaveResult objects with success/error info.
Governor limits:
- 150 DML statements per transaction.
- 10,000 records per DML in sync; higher async.
- Each statement counts as one regardless of record count.
Best practices:
- Bulkify: do one
insert listOfXinstead ofinsert xin a loop. Loops with DML inside hit the 150 limit fast. - Don't mix sObject types in one Database method call — that's a different limit.
- Use upsert with External Id for integrations to avoid duplicate-key issues.
DML inside a loop is the #1 anti-pattern in Apex.
