{"id":1503,"date":"2026-09-05T15:00:00","date_gmt":"2026-09-05T09:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/"},"modified":"2026-09-05T15:00:00","modified_gmt":"2026-09-05T09:30:00","slug":"dml-operations-in-apex","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/","title":{"rendered":"DML Operations in Apex: Insert, Update, Upsert and Delete"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">DML Operations in Apex are the statements that create, change and remove records in the Salesforce database. DML stands for Data Manipulation Language. Until now, we have only read data with SOQL and SOSL. Now we will write it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will learn each DML statement, how to work in bulk with lists, and how to handle errors and limits. It continues from <a href=\"https:\/\/swapnilzambare.com\/blog\/sosl-in-salesforce-search-apex\/\">SOSL in Salesforce<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/soql-relationship-queries-parent-child\/\">SOQL relationship queries<\/a>. It also uses what you learned about <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-sobjects-generic-vs-specific\/\">Apex sObjects<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are DML Operations in Apex?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">DML Operations in Apex are keywords that send records to the database. The main ones are insert, update, upsert, delete, undelete and merge. Each one works on a single record or, much better, on a list of records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a school office register. You can add a new student, correct a student&#8217;s address, remove a student who left, or bring back a record removed by mistake. DML statements are those register actions for your Salesforce data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why DML Operations in Apex Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Almost every automation eventually saves data. Here is why DML Operations in Apex matter at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, a class creates enrolments when a student pays.<\/li>\n\n<li>Also, a trigger updates the course&#8217;s seat count after each enrolment.<\/li>\n\n<li>Next, an integration upserts courses from the training system every night.<\/li>\n\n<li>A clean-up job deletes test enrolments older than a year.<\/li>\n\n<li>Finally, careless DML inside loops quickly breaks governor limits.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So correct, bulk-safe DML keeps data accurate and code within limits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How DML Operations in Apex Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">DML Operations in Apex follow a clear path. First, your code builds or changes sObjects in memory. Next, a DML statement sends them to the database. Then Salesforce runs validation rules, triggers, flows and other automation in the order of execution. After that, the records are saved, and new records get Ids. Finally, if anything fails, Salesforce throws a DmlException and rolls back the whole statement.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-1-1024x640.png\" alt=\"DML Operations in Apex diagram showing build, collect, DML, automation, save and error steps\" class=\"wp-image-1501\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">What happens when Apex runs a DML statement.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">insert<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">insert creates new records. After it runs, each record&#8217;s Id field is filled, so you can use it straight away.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">update<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">update changes existing records. Each record must have an Id, usually from a query.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">upsert<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">upsert inserts new records and updates existing ones in one statement. It matches on Id or on an external ID field, such as Course_Code__c.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">delete and undelete<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">delete moves records to the Recycle Bin. undelete brings them back, which needs a query with ALL ROWS to find deleted records.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">merge<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">merge combines up to three records of the same type into one. It works on objects such as Account, Contact, Lead and Case.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DML Statements at a Glance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below summarises each statement.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Statement<\/th><th>What it does<\/th><th>Needs Id<\/th><\/tr><\/thead><tbody><tr><td>insert<\/td><td>Creates new records<\/td><td>No, Id is created<\/td><\/tr><tr><td>update<\/td><td>Changes existing records<\/td><td>Yes<\/td><\/tr><tr><td>upsert<\/td><td>Inserts or updates by Id or external ID<\/td><td>Optional<\/td><\/tr><tr><td>delete<\/td><td>Moves records to the Recycle Bin<\/td><td>Yes<\/td><\/tr><tr><td>undelete<\/td><td>Restores deleted records<\/td><td>Yes<\/td><\/tr><tr><td>merge<\/td><td>Combines up to three duplicates<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Single-Record DML vs Bulk DML<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares DML on one record with DML on a list.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>One record per statement<\/th><th>List in one statement<\/th><\/tr><\/thead><tbody><tr><td>DML statements used for 200 records<\/td><td>200<\/td><td>1<\/td><\/tr><tr><td>Hits the 150 statement limit<\/td><td>Quickly<\/td><td>Rarely<\/td><\/tr><tr><td>Speed<\/td><td>Slow<\/td><td>Fast<\/td><\/tr><tr><td>Works in triggers<\/td><td>Risky<\/td><td>Safe<\/td><\/tr><tr><td>Error handling<\/td><td>One record at a time<\/td><td>Whole list, or partial with Database methods<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is clear. Always collect records in a list and run one DML statement. Single-record DML is fine only for one-off scripts.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-2-1024x640.png\" alt=\"DML Operations in Apex diagram comparing single-record DML and bulk DML\" class=\"wp-image-1502\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">One record per statement vs bulk DML.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real DML Operations in Apex Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s use DML Operations in Apex at Bright Path Tutors in the Execute Anonymous window.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Insert Records in Bulk<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Course__c&gt; newCourses = new List&lt;Course__c&gt;();\nfor (Integer i = 1; i &lt;= 3; i++) {\n    newCourses.add(new Course__c(\n        Name = 'SOQL Workshop ' + i,\n        Fee__c = 3000 + (i * 500),\n        Level__c = 'Intermediate',\n        Is_Online__c = true\n    ));\n}\ninsert newCourses;\nSystem.debug('First new Id: ' + newCourses[0].Id);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop builds three courses in memory only. Then a single insert saves all three, using one DML statement. Afterwards, each record has its Id.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Update in Bulk<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Course__c&gt; toUpdate = [\n    SELECT Id, Fee__c FROM Course__c WHERE Name LIKE 'SOQL Workshop%'\n];\nfor (Course__c c : toUpdate) {\n    c.Fee__c = c.Fee__c * 1.10;   \/\/ 10% price rise\n}\nupdate toUpdate;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We query the records, change the fee in memory, and update the whole list once. The loop contains no DML, which keeps the code bulk-safe.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Upsert by External ID<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Course__c&gt; fromTrainingSystem = new List&lt;Course__c&gt;{\n    new Course__c(Course_Code__c = 'APX-101', Name = 'Apex Basics', Fee__c = 4500),\n    new Course__c(Course_Code__c = 'LWC-201', Name = 'LWC Basics', Fee__c = 6000)\n};\nupsert fromTrainingSystem Course_Code__c;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Course_Code__c is an external ID field. Salesforce updates courses whose code already exists and inserts the others. This is ideal for nightly integrations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Handle Errors<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Course__c bad = new Course__c(Fee__c = 1000);  \/\/ Name is missing\n    insert bad;\n} catch (DmlException e) {\n    System.debug('Save failed: ' + e.getDmlMessage(0));\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If a required field or validation rule fails, insert throws a DmlException. The catch block reads the first error message, so the code can log it instead of crashing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Read the Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of the debug log.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USER_DEBUG [10]|DEBUG|First new Id: a015g00000AbC12AAB\nUSER_DEBUG [5]|DEBUG|Save failed: Required fields are missing: [Name]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">DML Operations in Apex and Governor Limits<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For DML Operations in Apex, a transaction can run 150 DML statements and process 10,000 records through DML. A DML statement on a list of 200 records counts as one statement but 200 rows. So the rule is simple: never put DML inside a loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Mixed DML Errors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some objects, such as User and PermissionSetAssignment, are setup objects. If one transaction changes a setup object and a normal object, such as Course__c, Salesforce throws a mixed DML error. The usual fix is to move one of the changes into asynchronous code, which we cover in the async level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with DML Operations in Apex<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Running insert or update inside a for loop.<\/li>\n\n<li>Updating records that were never queried, so Ids are missing.<\/li>\n\n<li>Forgetting that one bad record fails the whole list with plain DML.<\/li>\n\n<li>Ignoring DmlException, so users see technical errors.<\/li>\n\n<li>Mixing setup and non-setup objects in one transaction.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for DML Operations in Apex<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Collect records in lists and run one DML statement per object.<\/li>\n\n<li>Use upsert with an external ID for integrations.<\/li>\n\n<li>Catch DmlException and show clear messages.<\/li>\n\n<li>Use Database methods when you need partial success.<\/li>\n\n<li>Check Limits.getDmlStatements() in complex code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write code that creates one Enrolment__c for each of three contacts in a course, using one insert. Hint: query the contacts first, build the enrolments in a loop, then insert the list after the loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on DML Operations in Apex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Questions on DML Operations in Apex are part of almost every Apex interview. Here are the 20 asked most, with short answers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on DML Operations in Apex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is DML in Salesforce?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">DML means Data Manipulation Language. It includes statements that create, change and remove records, such as insert and update.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: Name the DML statements in Apex.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">insert, update, upsert, delete, undelete and merge.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: When does a new record get its Id?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Right after the insert statement succeeds. So you can use the Id on the next line.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What does upsert do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It inserts records that do not exist and updates those that do, matching on Id or an external ID field.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Where do deleted records go?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To the Recycle Bin, where undelete can restore them for a limited time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: Which objects support merge?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, Account, Contact, Lead and Case. Merge combines up to three records of the same type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: What exception does failed DML throw?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A System.DmlException, which you can catch with try and catch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on DML Operations in Apex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: How many DML statements can a transaction run?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">150 DML statements per transaction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: How many records can DML process in a transaction?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">10,000 rows in total across all DML statements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Why should DML not be inside a loop?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each loop pass uses another DML statement, so you quickly hit the 150 limit. Instead, collect records and run one statement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: What happens when one record in a list fails during insert?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With plain DML, the whole statement fails and nothing is saved. However, Database.insert with allOrNone false can save the good records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How do you upsert by an external ID?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Write upsert recordList ExternalField__c, for example upsert courses Course_Code__c.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: How do you restore a deleted record?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Query it with ALL ROWS and IsDeleted = true, then call undelete on it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: How do you read the error message from a DmlException?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use methods such as getDmlMessage(0), getDmlFieldNames(0) and getNumDml().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on DML Operations in Apex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: What is a mixed DML error?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It happens when one transaction changes setup objects, such as User, and normal objects together. So move one change into async code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Does DML run triggers and validation rules?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. DML starts the full order of execution, including validation rules, triggers and flows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: What is the difference between DML statements and Database methods?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">DML statements fail completely on any error. In contrast, Database methods can allow partial success and return result objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: Can you update a record without querying it first?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, if you know its Id. For example, create new Course__c(Id = knownId, Fee__c = 5000) and update it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How do you check DML usage in code?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use Limits.getDmlStatements() and Limits.getDmlRows() with their limit methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: How would you load 50,000 records in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use Batch Apex, because one transaction can process only 10,000 DML rows.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_dml_section.htm\" target=\"_blank\" rel=\"noopener noreferrer\">DML Statements (Apex Developer Guide)<\/a><\/li>\n\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_database\/apex_database_dml\" target=\"_blank\" rel=\"noopener noreferrer\">Trailhead: Manipulate Records with DML<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>DML Operations in Apex create, change and remove records.<\/li>\n\n<li>The statements are insert, update, upsert, delete, undelete and merge.<\/li>\n\n<li>Always use lists and one DML statement per object.<\/li>\n\n<li>The limits are 150 DML statements and 10,000 rows per transaction.<\/li>\n\n<li>Catch DmlException and avoid mixed DML errors.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Plain DML is all or nothing. Next, learn to save the good records and report the bad ones in our guide to the <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-database-class-partial-success\/\">Apex Database class and partial success<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DML Operations in Apex are the statements that create, change and remove records in the Salesforce database. DML stands for Data Manipulation Language. Until&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1500,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[98,107,26,91,7,97],"swapz_series":[],"class_list":["post-1503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-apex","tag-dml","tag-intermediate","tag-interview-questions","tag-salesforce","tag-salesforce-developer"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>DML Operations in Apex: Insert, Update, Upsert<\/title>\n<meta name=\"description\" content=\"DML Operations in Apex explained simply: insert, update, upsert, delete, undelete, merge, bulk lists, limits, errors and 20 interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"DML Operations in Apex: Insert, Update, Upsert\" \/>\n<meta property=\"og:description\" content=\"DML Operations in Apex explained simply: insert, update, upsert, delete, undelete, merge, bulk lists, limits, errors and 20 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-05T09:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"DML Operations in Apex: Insert, Update, Upsert and Delete\",\"datePublished\":\"2026-09-05T09:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/\"},\"wordCount\":1592,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/dml-operations-in-apex-featured.png\",\"keywords\":[\"Apex\",\"DML\",\"Intermediate\",\"Interview Questions\",\"Salesforce\",\"Salesforce Developer\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/\",\"name\":\"DML Operations in Apex: Insert, Update, Upsert\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/dml-operations-in-apex-featured.png\",\"datePublished\":\"2026-09-05T09:30:00+00:00\",\"description\":\"DML Operations in Apex explained simply: insert, update, upsert, delete, undelete, merge, bulk lists, limits, errors and 20 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/dml-operations-in-apex-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/dml-operations-in-apex-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"DML Operations in Apex featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/dml-operations-in-apex\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"DML Operations in Apex: Insert, Update, Upsert and Delete\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"DML Operations in Apex: Insert, Update, Upsert","description":"DML Operations in Apex explained simply: insert, update, upsert, delete, undelete, merge, bulk lists, limits, errors and 20 interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/","og_locale":"en_US","og_type":"article","og_title":"DML Operations in Apex: Insert, Update, Upsert","og_description":"DML Operations in Apex explained simply: insert, update, upsert, delete, undelete, merge, bulk lists, limits, errors and 20 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-05T09:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"DML Operations in Apex: Insert, Update, Upsert and Delete","datePublished":"2026-09-05T09:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/"},"wordCount":1592,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-featured.png","keywords":["Apex","DML","Intermediate","Interview Questions","Salesforce","Salesforce Developer"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/","url":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/","name":"DML Operations in Apex: Insert, Update, Upsert","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-featured.png","datePublished":"2026-09-05T09:30:00+00:00","description":"DML Operations in Apex explained simply: insert, update, upsert, delete, undelete, merge, bulk lists, limits, errors and 20 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/dml-operations-in-apex-featured.png","width":2400,"height":1260,"caption":"DML Operations in Apex featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"DML Operations in Apex: Insert, Update, Upsert and Delete"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1503"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1503\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1500"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1503"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}