Salesforce Apex Tutorial Chapter 16: DML In Apex

Salesforce Apex Tutorial Chapter 16: DML In Apex

On November 17, 2021, Posted by , In Salesforce Apex Tutorial, With Comments Off on Salesforce Apex Tutorial Chapter 16: DML In Apex

Apex is a data-driven language registered on the Lightning Platform. Therefore, it has direct access to your data in Salesforce. Unlike other programming languages ​​that require additional configuration to connect to data sources. DML stands for Data Manipulation Language which lets you Insert, Update, Delete, Undelete records in Apex. DML allows you to perform operations on a single sObject record or in bulk on a list of sObjects records at once. It is efficient to use sObjects lists for processing records. DML is the key part of Apex since almost every business case requires updating databases.

Let’s take a look at some examples of  DML operations like Insert, Update, Delete, etc.

Insert Operation: The insert operation inserts new records into the database. Using the Insert DML statement, you can create records of any Standard or Custom object.

For example: In this example, we are inserting an account record and two contact records related to that account.

// Create the account sObject 
Account accObject = new Account();
accObject .Name = ‘Test Account’;
accObject .Website =’www.salesforce.com’;
// Insert the account by using DML
Insert accObject ; 
// Create the contact sObject List
List<Contact> contactList = new List<Contact>();
// Create the contact sObject 
Contact con1 = new Contact();
con1.FirstName =’first’;
con1.LastName =’contact’;
con1.AccountId =’accObject.Id’;
// Adding the sObject to the list
contactList.add(con1);
// Create the contact sObject 
Contact con2 = new Contact();
con2.FirstName =’second’;
con2.LastName =’contact’;
con2.AccountId =’accObject.Id’;
// Adding the sObject to the list
contactList.add(con2);
// Insert the contact list by using DML
Insert contactList; 

Update Operation: Update operations are used to update existing records stored in the database. You have to query the existing records from the database to perform an update operation on them.

Example: In this example, we are querying the account which we have inserted in the above insert operation example and will update the name field on it.

Account accountRecord  =[Select Id, Name from Account where Name = ‘Test Account’ LIMIT 1];
accountRecord.Name = ‘Updated Name’;
// updating the account record using DML
update accountRecord; 
// To verify the account name whether it is updated or not
Account accountRecord =[Select Id, Name from Account where Name = ‘Updated Name’ LIMIT 1];
// Display the account record in the debug logs
system.debug(‘account afterUpdate::::’+accountRecord);

Delete Operation: Delete operations are performed on the existing records in the database. Deleted records are not deleted permanently from salesforce; they are stored for 15 days in the recycle bin; from there they can be restored within 15 days.

Example: In this example, we delete the account record which we updated in the above update operation example.

// Querying the account record
Account accn =[Select Id, Name from Account where Name = ‘Updated Name’ LIMIT 1];
// Deleting the account record using DML
Delete accn;
// To verify whether account record is deleted or not
Account accn =[Select Id, Name from Account where Name = ‘Updated Name’ LIMIT 1];
if(accn.size() ==0){
// Display the message in the debug logs
system.debug(‘account does not exist’);
}else{
// Display the message in the debug logs
system.debug(‘account existed’);
}

Undelete Operation: When you delete the records, they are stored in the recycle bin for 15 days and after 15 days they are deleted permanently from the salesforce. To query the deleted records from the recycle bin, we have to use the ALL ROWS parameter in the Soql query.

Example: In this example, we are retrieving account records that we have deleted in the above delete operation example.

// Querying the account record
Account accn = [select Id, Name from Account where Name =’Updated Name’ ALL ROWS];
// Undeleting the account record using DML
Undelete accn ;
// To verify whether account record is restored or not
Account accn = [select Id, Name from Account where Name =’Updated Name’];
// Display the account record in the debug logs
system.debug(‘restored account record:::’+accn);

Upsert Operation: Upsert statements allow you to insert new records or update existing records in a single call. Upsert statements determine whether a record already exists or not by using the record’s ID.

Example: In this example, we are updating the account name of an existing account record from ‘Updated Name’ to ‘salesforce Account’ and also creating a new account record.

// Querying the account records
Account[] acctsList = [SELECT Id, Name, BillingCity
FROM Account WHERE Name=’Updated Name’];
//Iterating over the account records
for (Account a : acctsList) {
a.Name =’Salesforce account’;
}
// Creating a new record
Account newAcct = new Account(Name =’SF Account’);
// Adding the new record to the list
acctsList.add(newAcct);
// Upserting the account List using DML
Upsert acctsList;
Comments are closed.