Intermediate 15 min read

What Is DML?

By the end of this lesson, you'll be able to:

  • Explain what DML means and what it does to the actual Salesforce database
  • Name the four core DML statements
  • Distinguish an in-memory sObject change from a saved database change

Prerequisites: Module 18: "Understanding Salesforce Data in Apex"

DML makes a change permanent

Account acc = new Account(Name = 'Acme Logistics');
// At this point, acc only exists in memory — nothing is saved yet.

insert acc;
// Now acc is a real, permanent row in the Account table.
  1. Account acc = new Account(Name = 'Acme Logistics'); — every sObject you've created so far in this course — back to Module 18's new Account() — only exists in memory at this point.
  2. // At this point, acc only exists in memory — nothing is saved yet. — spells it out.
  3. (blank line)
  4. insert acc; — DML (Data Manipulation Language) is what actually writes a change to the real Salesforce database.
  5. // Now acc is a real, permanent row in the Account table. — permanent and visible to every other part of the org.

The four core statements, at a glance

insert acc;  // create a brand-new record
update acc;  // change an existing record
delete acc;  // remove an existing record
upsert acc;  // insert if new, update if it already exists
  1. insert acc; — creates a brand-new record.
  2. update acc; — changes an existing record.
  3. delete acc; — removes an existing record.
  4. upsert acc; — inserts if new, updates if it already exists.

Each of the next several lessons goes deep on one of these — this lesson is the map of the territory, not the full detail on any one statement yet.

Exercise

As a comment, explain the difference between creating a new Account() in Apex and running insert on it.

Show hint

Think about what "permanent" means here.

APEX

What Is DML? Quiz

1. What does DML actually do?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

insert, update, delete, and upsert are Apex's four core DML statements. Wrapping them in try/catch lets you handle validation failures and other DmlExceptions gracefully.