• WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Implementations
        • Salesforce
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service for SMEs
          • Field Service Lightning
        • Salesforce Partner Apps
          • Panda Doc
          • Conga
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • HOW WE DO
      • Delivery Model
      • Our Works
    • BLOG
Salesforce Blog | Salesforce Simple Tutorial
Salesforce Blog | Salesforce Simple Tutorial
  • WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • BLOG

Overview of DML operations in Apex

Salesforce Blog | Salesforce Simple Tutorial > Salesforce Admin Tutorial > Overview of DML operations in Apex
  • January 24, 2023
  • Merfantz Blogger
  • Salesforce Admin Tutorial
  • 0

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

Using DML, you can insert new records and commit them to the database. You can also update the field values of existing records.
  • Inserting and Updating Records
  • Upserting Records
  • Merging Records
  • Deleting Records
  • Restoring Deleted Records
  • Converting Leads

1.INSERT:

Overall, used to create a new record in Salesforce. Developers can use the INSERT statement to insert a single record or a list of records at once. The INSERT statement can be used to create new records for standard and custom objects in Salesforce. For example, a developer can use the INSERT statement to create a new account, contact, or custom object in Salesforce.

  • 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. 


  • Previous Salesforce Marketing Cloud Will Fuel Your B2B Marketing Engine.
  • Next Benefits of Integrating Salesforce and Marketing Automation for Business Growth
We denounce with righteous indignation in and dislike men who are so beguiled and to demo realized by the charms of pleasure moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound.

Discover More

Contact Info

  • 374 William S Canning Blvd, Fall River MA 2721, USA
  • (+880)155-69569
  • [email protected]
  • 10:00 - 17:00

Latest Posts

Salesforce Marketing Cloud Will Fuel Your B2B Marketing Engine. January 30, 2023
Overview of DML operations in Apex January 24, 2023
Benefits of Integrating Salesforce and Marketing Automation for Business Growth January 20, 2023

2023 All Rights Reserved