What is DML in apex?
Apex that allows you to add and manage records in the database is the Data Manipulation Language. Apex offers two ways to perform DML operations: using DML statements or Database class methods. Basically, allow developers to insert, update, and delete records in Salesforce. Altogether, the four main DML operations in Apex are INSERT, UPDATE, UPSERT, and DELETE. Each operation serves a specific purpose. After that developers can use them to perform different tasks depending on their needs.
DML Operations
- Example of Inserting Only One Record
- Fetch the Id of the Inserted Record
- Inserting Records in Bulk using List
- Following Governor Limit Check
- Inserting two records separately then two DML will be counted.
- But if we insert two records through a list then one DML will be counted.
- Inserting Related Records, for example first insert account and then contact.
2. UPDATE:
To modify existing records in Salesforce. Developers moreover use the UPDATE statement to update one or multiple records at a time. For example, a developer can use the UPDATE statement to change the name of an account or update the value of a custom field.
- Query an Existing Record and Print its Values!
- Then, Update the Record and do Update.
- Now, Query the Updated Record
- Now apply System.assertEqual to Validate Update
- Update Related Records
3.UPSERT:
This operation is a combination of the INSERT and UPDATE operations. Developers meanwhile can use the UPSERT statement to either insert a new record or update an existing record, depending on whether the record already exists in Salesforce. UPSERT is particularly useful when working with large amounts of data and you want to ensure that there are no duplicates in the system.
Merge Records
- So only lead, Contact and Account records can be merged.
- We can only merge up to three records of the same sObject type into one.
- For example, if three leads have the same value then we can merge them into one same goes for contact and account records.
- After merging the records, the old record will be deleted and merged into a new one.
So, record 1 has related contact and record 2 does not have any related contact then if we merge them then we can have related contact in record 2 as well.
4. DELETE:
Developers can use the DELETE statement to delete a single record or a list of records at once. Then, the DELETE statement can be used to delete records for standard and custom objects in Salesforce. For example, a developer can use the DELETE statement to delete an account, contact, or custom object in Salesforce.
Undelete Records
- Insert a record.
- Delete that record.
- Now query deleted records using all rows in the query.
All rows will query all the records whether they are deleted or undeleted.
- Then apply undelete statement.
DML Statement Exception
Apply to try and catch to handle exceptions raised when DML operations fail.
So, this will show all the DML errors which will appear while performing DML operations inside the org. Such as here, in this case, we can see that the account record is created without a name so it will show an error.