{"id":1527,"date":"2026-09-06T15:00:00","date_gmt":"2026-09-06T09:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/"},"modified":"2026-09-06T15:00:00","modified_gmt":"2026-09-06T09:30:00","slug":"before-vs-after-triggers-apex","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/","title":{"rendered":"Before vs After Triggers in Apex: When to Use Each"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Before vs After Triggers is one of the first design choices in every trigger you write. A before trigger runs before Salesforce saves the record, so it is perfect for changing or checking the record&#8217;s own fields. An after trigger runs once Salesforce saves the record, so it is right for working with other records that need its Id.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will learn exactly what each timing can and cannot do, see the same business rule built both ways, and learn a simple rule for picking the right one. It builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-trigger-basics-context-variables\/\">Apex Trigger Basics<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/\">DML operations in Apex<\/a>. It also uses the <a href=\"https:\/\/swapnilzambare.com\/blog\/salesforce-order-of-execution\/\">Salesforce order of execution<\/a> to explain when each trigger runs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Before vs After Triggers?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A before trigger runs before Salesforce writes the record to the database. At that moment, you can still change the values in Trigger.new, and Salesforce saves your changes with no extra DML. An after trigger runs after Salesforce saves the record but before the transaction commits. At that point, the record has its Id and system fields, but Trigger.new is read only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of posting a parcel. Before you seal the box, you can still add or remove items, which is a before trigger. Once you seal the box and the courier adds a tracking number, you can only use that number to tell other people about it, which is an after trigger.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Before vs After Triggers Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With Before vs After Triggers, picking the wrong timing causes errors, extra DML and slower saves. Here is why Before vs After Triggers matter at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, setting Amount Due on a new enrolment belongs in before insert.<\/li>\n\n<li>Also, creating a welcome task for a new enrolment needs its Id, so it belongs in after insert.<\/li>\n\n<li>Next, blocking a fee drop larger than 20% belongs in before update.<\/li>\n\n<li>Updating the parent course&#8217;s seat count belongs in after insert and after delete.<\/li>\n\n<li>Finally, the wrong choice can mean an extra update that runs triggers again.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So the right timing gives simpler, faster and safer code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Before vs After Triggers Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before vs After Triggers sit at different points of the save. First, Salesforce loads the record and runs system validation. Next, before-save flows and before triggers run, and they can still change the record. Then Salesforce runs validation rules and duplicate rules, and saves the record to the database without committing. After that, after triggers run, with the record&#8217;s Id available. Finally, other automation runs and the transaction commits.<\/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\/before-vs-after-triggers-apex-diagram-1-1024x640.png\" alt=\"Before vs After Triggers diagram showing load, before, validate, save, after and commit steps\" class=\"wp-image-1525\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Where before and after triggers run during a save.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Before Triggers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use before triggers to set or correct fields on the records in the save, and to validate them with addError. Changes go straight into the save with no DML.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">After Triggers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use after triggers to create or update other records, such as child records, parent totals or tasks. They also suit work that needs the record Id, CreatedDate or other system values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Ids and System Fields<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In before insert, new records have no Id yet. Then, in after insert, every record has its Id, so you can link related records to it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Read-Only Records in After Triggers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By contrast, after triggers treat Trigger.new as read only. To change the same record, you would need a new DML statement, which runs the triggers again. So set your own fields in before triggers instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Before vs After Triggers at a Glance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows which jobs belong to each timing.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Job<\/th><th>Before trigger<\/th><th>After trigger<\/th><\/tr><\/thead><tbody><tr><td>Set fields on the same record<\/td><td>Yes, best choice<\/td><td>Needs extra DML, avoid<\/td><\/tr><tr><td>Validate with addError<\/td><td>Yes, best choice<\/td><td>Possible, but later<\/td><\/tr><tr><td>Use the new record Id<\/td><td>No Id in insert<\/td><td>Yes<\/td><\/tr><tr><td>Create child or related records<\/td><td>Possible but no Id in insert<\/td><td>Yes, best choice<\/td><\/tr><tr><td>Update parent records<\/td><td>Possible<\/td><td>Yes, best choice<\/td><\/tr><tr><td>Trigger.new editable<\/td><td>Yes<\/td><td>No, read only<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Before vs After Triggers Compared<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares the two timings on key points.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Before trigger<\/th><th>After trigger<\/th><\/tr><\/thead><tbody><tr><td>Runs<\/td><td>Before the save<\/td><td>After the save, before commit<\/td><\/tr><tr><td>Record Id in insert<\/td><td>Not available<\/td><td>Available<\/td><\/tr><tr><td>Edit Trigger.new<\/td><td>Allowed<\/td><td>Not allowed<\/td><\/tr><tr><td>DML on the same records<\/td><td>Not needed<\/td><td>Needed, and re-runs triggers<\/td><\/tr><tr><td>Typical job<\/td><td>Defaults and validation<\/td><td>Related records and roll-ups<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which comes down to one question: does the change affect this record or another record? Use before for this record. Use after for other records.<\/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\/before-vs-after-triggers-apex-diagram-2-1024x640.png\" alt=\"Before vs After Triggers diagram comparing before triggers and after triggers\" class=\"wp-image-1526\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Before trigger vs after trigger.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Before vs After Triggers Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s build one Enrolment__c trigger at Bright Path Tutors that uses both timings. Before insert sets Amount Due. After insert creates a welcome task and updates the course&#8217;s booked seats.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Trigger<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>trigger EnrolmentTrigger on Enrolment__c (before insert, after insert) {\n\n    if (Trigger.isBefore &amp;&amp; Trigger.isInsert) {\n        EnrolmentLogic.setAmountDue(Trigger.new);\n    }\n\n    if (Trigger.isAfter &amp;&amp; Trigger.isInsert) {\n        EnrolmentLogic.createWelcomeTasks(Trigger.new);\n        EnrolmentLogic.updateSeatsBooked(Trigger.new);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The trigger only decides which method runs for each timing. The real work sits in a class, which keeps the trigger short and easy to read.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: The Logic Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class EnrolmentLogic {\n\n    \/\/ BEFORE: change the records being saved, no DML needed\n    public static void setAmountDue(List&lt;Enrolment__c&gt; rows) {\n        Set&lt;Id&gt; courseIds = new Set&lt;Id&gt;();\n        for (Enrolment__c e : rows) { courseIds.add(e.Course__c); }\n        Map&lt;Id, Course__c&gt; courses = new Map&lt;Id, Course__c&gt;(\n            [SELECT Id, Fee__c FROM Course__c WHERE Id IN :courseIds]);\n        for (Enrolment__c e : rows) {\n            if (courses.containsKey(e.Course__c)) {\n                e.Amount_Due__c = courses.get(e.Course__c).Fee__c;\n            }\n        }\n    }\n\n    \/\/ AFTER: records now have Ids, so link new tasks to them\n    public static void createWelcomeTasks(List&lt;Enrolment__c&gt; rows) {\n        List&lt;Task&gt; tasks = new List&lt;Task&gt;();\n        for (Enrolment__c e : rows) {\n            tasks.add(new Task(WhatId = e.Id, Subject = 'Send welcome kit',\n                               ActivityDate = Date.today().addDays(2)));\n        }\n        insert tasks;\n    }\n\n    \/\/ AFTER: update parent courses with a fresh count\n    public static void updateSeatsBooked(List&lt;Enrolment__c&gt; rows) {\n        Set&lt;Id&gt; courseIds = new Set&lt;Id&gt;();\n        for (Enrolment__c e : rows) { courseIds.add(e.Course__c); }\n        List&lt;Course__c&gt; toUpdate = new List&lt;Course__c&gt;();\n        for (AggregateResult ar : [SELECT Course__c cId, COUNT(Id) n FROM Enrolment__c\n                                   WHERE Course__c IN :courseIds GROUP BY Course__c]) {\n            toUpdate.add(new Course__c(Id = (Id) ar.get('cId'),\n                                       Seats_Booked__c = (Integer) ar.get('n')));\n        }\n        update toUpdate;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">setAmountDue changes the enrolments directly, with no DML, because it runs before the save. createWelcomeTasks needs each enrolment Id for WhatId, so it must run after insert. updateSeatsBooked changes other records, the parent courses, so it also belongs in after insert.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Read the Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After inserting three enrolments, a quick check in Execute Anonymous shows results like these.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USER_DEBUG [3]|DEBUG|Amount Due set: 4500\nUSER_DEBUG [8]|DEBUG|Welcome tasks created: 3\nUSER_DEBUG [9]|DEBUG|Apex Basics seats booked: 21<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Before vs After Triggers for Validation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With Before vs After Triggers, you can validate in both timings, but before triggers are usually better. They stop bad data early, before other automation runs. Use after triggers for validation only when the rule needs data that exists after the save, such as checking a total across all child records.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Before vs After Triggers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Running update on Trigger.new inside an after trigger to change a field.<\/li>\n\n<li>Trying to use record Ids in before insert.<\/li>\n\n<li>Creating child records in before insert, where the parent has no Id.<\/li>\n\n<li>Putting parent updates in before triggers, so they run before the save is valid.<\/li>\n\n<li>Forgetting after delete and after undelete when keeping totals correct.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Before vs After Triggers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set and validate this record&#8217;s fields in before triggers.<\/li>\n\n<li>Create or update other records in after triggers.<\/li>\n\n<li>Keep totals right across insert, update, delete and undelete.<\/li>\n\n<li>Keep trigger bodies short and move logic into classes.<\/li>\n\n<li>Ask one question every time: this record or another record?<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add an after delete branch that recalculates Seats_Booked__c when enrolments are deleted. Hint: use Trigger.old to collect the course Ids, then reuse updateSeatsBooked.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on Before vs After Triggers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers love Before vs After Triggers because the topic shows whether you understand the save process. Here are the 21 questions asked most about Before vs After Triggers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on Before vs After Triggers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is the difference between before and after triggers?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before triggers run before Salesforce saves the record and can change its fields. After triggers run after the save and can use its Id.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: When would you use a before trigger?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To set default values, correct data or validate fields on the same records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: When would you use an after trigger?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To create or update related records, or when you need the record Id or system fields.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: Can you get the record Id in before insert?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. New records get their Ids only after Salesforce saves them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Is Trigger.new editable in after triggers?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. It is read only in after triggers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: Do you need DML to change fields in a before trigger?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Changes to Trigger.new save automatically with the record.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: Which events have only an after version?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Undelete. There is only an after undelete event.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on Before vs After Triggers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Where should you create a task for a new record?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In after insert, because the task needs the record Id for WhatId.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: Where should you update a parent roll-up value?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In after triggers on the child, so the child record is already saved.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: What happens if you update the same record in its after trigger?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It runs a new DML, which fires the triggers again and can cause recursion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: Where should a fee validation rule in Apex go?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a before trigger, using addError, so bad data stops early.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: Can before triggers do DML on other objects?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, but after triggers are usually better for other records, because the current save is already valid by then.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: How do you keep a parent count correct?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Recalculate it in after insert, after update, after delete and after undelete.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: Which context variables help in after update?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trigger.newMap and Trigger.oldMap, which let you compare values and use Ids.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on Before vs After Triggers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: When do before triggers run compared with validation rules?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before triggers run first. So validation rules check the values your before trigger set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Do after triggers see field values set by workflow?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Only in a later run. A workflow field update runs before and after update triggers one more time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Can an after trigger roll back the save?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. An unhandled exception or addError in an after trigger rolls back the transaction or the marked records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: Why are before triggers faster for field updates?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They need no extra DML. After triggers would need another update, which runs triggers and automation again.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: Where do before-save flows run compared with before triggers?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before-save flows run first, then before triggers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Can you see formula field values in a before trigger?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not reliably. Formula fields on Trigger.new are not recalculated during a before trigger. If you need fresh values, call Formula.recalculateFormulas on the records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: How do you choose between before and after quickly?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ask if the change is to this record or another record. This record means before, another record means after.<\/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_triggers.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Triggers (Apex Developer Guide)<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_triggers_order_of_execution.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Triggers and Order of Execution (Apex Developer Guide)<\/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>Before vs After Triggers depends on which record you change.<\/li>\n\n<li>Before triggers set and validate this record with no DML.<\/li>\n\n<li>After triggers use Ids and change other records.<\/li>\n\n<li>Trigger.new is read only in after triggers.<\/li>\n\n<li>Keep totals correct across insert, update, delete and undelete.<\/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\">Triggers must handle 200 records at once. Next, read our guide to <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-bulkification-best-practices\/\">Apex bulkification best practices<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before vs After Triggers is one of the first design choices in every trigger you write. A before trigger runs before Salesforce saves the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1524,"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,26,91,7,97,108],"swapz_series":[],"class_list":["post-1527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-apex","tag-intermediate","tag-interview-questions","tag-salesforce","tag-salesforce-developer","tag-triggers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Before vs After Triggers in Apex Explained<\/title>\n<meta name=\"description\" content=\"Before vs After Triggers explained simply: field updates, validations, related records, Ids, read-only rules, real Apex examples and 21 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\/before-vs-after-triggers-apex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Before vs After Triggers in Apex Explained\" \/>\n<meta property=\"og:description\" content=\"Before vs After Triggers explained simply: field updates, validations, related records, Ids, read-only rules, real Apex examples and 21 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-06T09:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-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=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"Before vs After Triggers in Apex: When to Use Each\",\"datePublished\":\"2026-09-06T09:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/\"},\"wordCount\":1733,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/before-vs-after-triggers-apex-featured.png\",\"keywords\":[\"Apex\",\"Intermediate\",\"Interview Questions\",\"Salesforce\",\"Salesforce Developer\",\"Triggers\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/\",\"name\":\"Before vs After Triggers in Apex Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/before-vs-after-triggers-apex-featured.png\",\"datePublished\":\"2026-09-06T09:30:00+00:00\",\"description\":\"Before vs After Triggers explained simply: field updates, validations, related records, Ids, read-only rules, real Apex examples and 21 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/before-vs-after-triggers-apex-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/before-vs-after-triggers-apex-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Before vs After Triggers featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/before-vs-after-triggers-apex\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Before vs After Triggers in Apex: When to Use Each\"}]},{\"@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":"Before vs After Triggers in Apex Explained","description":"Before vs After Triggers explained simply: field updates, validations, related records, Ids, read-only rules, real Apex examples and 21 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\/before-vs-after-triggers-apex\/","og_locale":"en_US","og_type":"article","og_title":"Before vs After Triggers in Apex Explained","og_description":"Before vs After Triggers explained simply: field updates, validations, related records, Ids, read-only rules, real Apex examples and 21 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-06T09:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"Before vs After Triggers in Apex: When to Use Each","datePublished":"2026-09-06T09:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/"},"wordCount":1733,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-featured.png","keywords":["Apex","Intermediate","Interview Questions","Salesforce","Salesforce Developer","Triggers"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/","url":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/","name":"Before vs After Triggers in Apex Explained","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-featured.png","datePublished":"2026-09-06T09:30:00+00:00","description":"Before vs After Triggers explained simply: field updates, validations, related records, Ids, read-only rules, real Apex examples and 21 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/before-vs-after-triggers-apex-featured.png","width":2400,"height":1260,"caption":"Before vs After Triggers featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/before-vs-after-triggers-apex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Before vs After Triggers in Apex: When to Use Each"}]},{"@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\/1527","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=1527"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1527\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1524"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1527"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}