Salesforce DML operations

Apex also gives you a way to insert, update, delete, or restore data in the database using DML. Using Salesforce DML operations, we can modify the records at one time or in a batch. It means we can insert one record of Account, or we can make a list of accounts and insert it in a single statement. This is best practice or advisable to perform Salesforce DML operation for bulk data because it will help us to avoid the governing limit of 150 DML a single transaction. 

Salesforce DML operation works in a system context mostly. This means it doesn’t depend on the user-based sharing rule most of the time. The user may have no access to perform DML operations from the profile. If a user is executing DML in an anonymous block of the developer console, then all the permission related to the user will apply to DML.

At this point, we should discuss the Data Manipulation Language (DML). DML is a series of statements and methods available in Apex that enable a developer to push data changes to the Salesforce database. The following keywords are all reserved (that is cannot be used as variable names) and operate on one or more records.

Insert 

The insert statement saves a new record to the Salesforce database. Note that after the insert is performed the Id field on the record is populated by the system: 

Contact newContact = new Contact(FirstName = 'Chukka', LastName = 'Sushanth'); 
insert newContact;

DML insert operation

Update 

The update statement saves changes to an existing record to the database, and the record must have the Id field populated.

Contact newContact = new Contact(FirstName = 'Chukka', LastName = 'Sushanth');
insert newContact; newContact.Email = 'test@test.email.com'; update newContact;

Upsert

 The upsert statement requires an external Id field to be populated which the system will use to match on. If a match is found an update is performed, otherwise a new record is inserted as if using the insert statement:

Contact newContact = new Contact(FirstName = 'Chukka', LastName = 'Sushanth', External_Id_Field__c = 'MyExternalId');
upsert newContact;

Delete 

The delete statement removes a record with the given Salesforce Id from the database:

Contact newContact = new Contact(FirstName = 'Chukka', LastName = 'Sushanth');
insert newContact;
delete newContact; //Contact is removed from database

Undelete 

The undelete statement restores records from the recycle bin. These records must be queried from the database. The query shown below retrieves a set of deleted contacts from the recycle bin with the LastName field equal to Smith.

undelete [SELECT Id, LastName FROM Contact WHERE LastName = 'Smith' ALL ROWS];

 Merge 

The merge statement merges up to 3 records into a single record and removes the others. The first record provided is the master record into which the others should be merged:

Contact newContact = new Contact(FirstName = 'Chukka', LastName = 'Sushanth');
insert newContact;
Contact newContact2 = new Contact(FirstName = 'Chukka', LastName = 'Sushanth');
insert newContact2;
merge newContact newContact2;

These 6 DML statements enable developers to change data on the Salesforce database and work with sObject instances that have been created or manipulated by our Apex code.