Modify Records with DML
Learning Objectives
After completing this unit, you’ll:
- Understand how to use the Query Grid in Developer Console to manipulate data records directly.
- Understand how to use DML to persist data to the database.
- Be able to identify two ways to write DML commands.
Manage Data in Developer Console
So far we’ve seen how to use Developer Console to query data using SOQL and SOSL. What you probably didn’t notice was that the Query Results pane displayed in Developer Console not only displays the results, but it functions as a query grid. You can click in the results grid and use the buttons below the grid to insert, update, or delete data.
Let’s give it a try.
- From Setup, select Your Name > Developer Console to open Developer Console.
- In Developer Console, click the Query Editor tab in the bottom pane.
- Delete the existing code, and insert the following snippet:
SELECT Id, Name, Industry FROM Account
- Click Execute. The query results displays all accounts in the org, along with a count of the total rows. Notice what this value is.
- Click Insert Row. A new row is added at the top of the grid.
- Double-click in the new row to enter a name and industry, but leave the ID value blank.
- Click Save Rows. Notice that a value automatically displayed in the Id column. Remember, the ID is the primary key and that value is system generated. Also notice that the total rows displayed is still the original value.
- Click Refresh Grid. ,Did you notice the total rows count increase by one? Not a bad little tool, huh?
As developers, we know that you sometimes have the need to manage data on the fly and we wanted to make it as easy for you as possible. We think it sure beats typing in an INSERT, UPDATE, or DELETE statement.
You can also use the buttons at the bottom right of the query results to access the records in Salesforce directly. Go ahead and give it a try.
What Is DML?
As you learned in the first unit, Salesforce uses SOQL to query data, but when it comes to managing the data, DML is used instead.
As a .NET developer, you primarily use SQL to query and manage data, so it could feel strange to use another language. But we think you’ll find working with the DML commands in Salesforce much easier than the SQL equivalents.
Because Force.com is so tightly integrated, you can get straight to the business of manipulating your data without worrying about managing a data source. That’s one reason why DML is so easy to work with.
DML Statements
Check out the table below to see which DML statements are available. Some probably look familiar to you, and they work pretty much as you would expect them to. See the Developer’s Guide for more information.
Statement | Description |
---|---|
insert | Inserts a new record. |
update | Updates an existing record. |
upsert | This is a special operation that creates new records and updates records within a single statement. It uses a specified field to determine the presence of existing objects, or the ID field if no field is specified. |
delete | Deletes an existing record. |
undelete | Restores one or more existing records from the recycle bin. |
merge | This is another special operation that can merge up to three records of the same object type into one of the records. It will then delete the other records and reparent any related records. |
Two Ways to Execute DML
In Apex, you can execute DML statements in two ways.
- Using the statement itself: You’ve already seen this method in the unit on SOSL in which we inserted Account and Contact data. This method is the easiest way. In case you don’t remember, here is some code from the SOSL unit. A single Account was inserted using the insert DML statement.
// Add Account Account acct = new Account( Name='Test Account', Phone='(225)555-8989', NumberOfEmployees=10, BillingCity='Baton Rouge'); insert acct;
- Using Database class methods: The advantage of using the Database class method is that you have access to the optional
allOrNone
parameter to specify whether the operation can partially succeed. When this parameter is set to false, if an error occurs on a partial set of records, the successful records are committed. Errors for the failed records are returned. Also, no exceptions are thrown with the partial success option. For example, to create an Account with theallOrNone
parameter set to false and return the success or failure info in an array, would look like the following.// Add Account Account acct = new Account( Name='Test Account', Phone='(225)555-8989', NumberOfEmployees=10, BillingCity='Baton Rouge'); Database.SaveResult[] results = Database.insert(acct, false);
Tell Me More
By default, the allOrNone parameter is true, which means that the Database method behaves like its DML counterpart and throws an exception if a failure is encountered. Check out the official docs for more information.
If you want to become a DML rockstar, complete the Apex Basics & Database module. It contains more information about how to insert records with partial success and how to insert related records.