,

Data Manipulation with Apex

At this point, you have used SOQL to retrieve a set of User records. The last piece of the data manipulation puzzle is the ability to write data through Data Manipulation Language (DML) syntax. Apex has a set of commands that make this final step very simple.

Apex DML Syntax

There are two ways to perform data manipulation requests with Apex. The simplest uses a simple DML statement for each different data manipulation action, including the following:

  • Insert - adds records to a Force Platform database object. The Force Platform automatically creates a unique ID for any record inserted.

  • Update - updates values in an existing record in a Force Platform database object, based on the ID of the record.

  • Delete - deletes records in a Force Platform object, based on the ID of the record. Deleting a master record in a master-detail relationship deletes all child records for that relationship.

  • Upsert - performs either an insert or update, based on the presence of an ID for the record.

  • Undelete - removes records from the Recycle bin and places them back into their original Force Platform object

Apex also supports the merge statement and convertLead statements, which are used for specific use cases and are detailed in the Apex Language Reference documentation.

As you will see in the example in the next section, all of these commands operate on a set of records.

As mentioned above, you can use methods on an instance of the Database class to perform the same data manipulation actions. These methods return a result object, with information about the execution of the operation. One key difference between using a simple DML statement and using a Database method is how you can handle errors with them. A simple DML statement either succeeds or fails. If any of the operations in a simple DML call fail, the entire call fails. With Database methods, you can specify that the overall operation continues if an individual operation on a specific record fails. With this flexibility, you can create database interactions that never throw an exception—you handle the exception circumstances yourself by analyzing the result object. Note that the merge action does not have a matching Database method.

For more information on Database methods, please refer to the Apex Language Reference documentation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.215.117