• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

Overview of DML operations in Apex

  • January 24, 2023
  • Merfantz Editor
  • 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. 

Author Bio

Merfantz Editor
+ Recent Posts
  • August 4, 2023
    How to Setup CRM Analytics for Financial Services Cloud
  • August 2, 2023
    How to Get Started with Salesforce Hyperforce - Merfantz
  • July 28, 2023
    Health Cloud Mastery: Salesforce Trailhead for Effective Healthcare CRM
  • July 26, 2023
    How to integrate Data cloud and Marketing cloud In Salesforce
  • Previous Salesforce Marketing Cloud Will Fuel Your B2B Marketing Engine.
  • Next Benefits of Integrating Salesforce and Marketing Automation for Business Growth
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Salesforce GraphQL Revolutionizing Data Queries in CRM
Salesforce GraphQL: Revolutionizing Data Queries in CRM June 6, 2025
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder May 29, 2025
Einstein Activity Capture Boosting Productivity & Relationship Intelligence
Einstein Activity Capture: Boosting Productivity & Relationship Intelligence May 22, 2025

Copyright @2023 Merfantz Technologies, All rights reserved