{"id":5317,"date":"2021-11-23T04:33:32","date_gmt":"2021-11-23T04:33:32","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5317"},"modified":"2021-11-23T05:15:14","modified_gmt":"2021-11-23T05:15:14","slug":"salesforce-triggers","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/salesforce-triggers\/","title":{"rendered":"Salesforce Triggers"},"content":{"rendered":"<p>A <strong>trigger <\/strong>is an <strong>Apex script<\/strong> that executes before or after data manipulation language (DML) events occur. Apex triggers enable us to perform custom actions before or after events to record in <a href=\"https:\/\/studysection.com\/blog\/start-with-salesforce-com\/\">Salesforce<\/a>, such as insertions, updates, or deletions.<\/p>\n<p>It is a concept of automating a process and we can solve the complex scenarios that we can\u2019t perform using process builder and workflow. Triggers work with DML operations.<\/p>\n<p>There are two types of triggers &#8211; <\/p>\n<ol>\n<li>Before &#8211; These are fired before inserting the record in the database.<\/li>\n<li>After &#8211; These are fired after inserting the record in the database.<\/li>\n<\/ol>\n<h2>Trigger events in salesforce<\/h2>\n<ul>\n<li>before insert<\/li>\n<li>before update<\/li>\n<li>before delete<\/li>\n<li>after insert<\/li>\n<li>after update<\/li>\n<li>after delete<\/li>\n<li>after undelete<\/li>\n<\/ul>\n<h3>Context variables in triggers &#8211;<\/h3>\n<p>To access the records that caused the trigger to fire, use context variables. For example, <em style=\"color:red;\">Trigger.New<\/em> contains all the records that were inserted in insert or update triggers. <em style=\"color:red;\">Trigger.Old<\/em> provides the old version of sObjects before they were updated in update triggers, or a list of deleted sObjects in delete triggers. Triggers can fire when one record is inserted, or when many records are inserted in bulk via the API or Apex. Therefore, context variables, such as <em style=\"color:red;\">Trigger.New<\/em>, can contain only one record or multiple records. We can iterate over <em style=\"color:red;\">Trigger.New<\/em> to get each individual sObject.<\/p>\n<ul>\n<li><strong>isExecuting:<\/strong> Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.<\/li>\n<li><strong>isInsert:<\/strong> Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.<\/li>\n<li><strong>isUpdate:<\/strong> Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.<\/li>\n<li><strong>isDelete:<\/strong> Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.<\/li>\n<li><strong>isBefore:<\/strong> Returns true if this trigger was fired before any record was saved.<\/li>\n<li><strong>isAfter:<\/strong> Returns true if this trigger was fired after all records were saved.<\/li>\n<li><strong>isUndelete:<\/strong> Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)<\/li>\n<li><strong>new:<\/strong> Returns a list of the new versions of the sObject records. This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.<\/li>\n<li><strong>newMap:<\/strong> A map of IDs to the new versions of the sObject records. This map is only available in before update, after insert, after update, and after undelete triggers.<\/li>\n<li><strong>old:<\/strong> Returns a list of the old versions of the sObject records. This sObject list is only available in update and delete triggers.<\/li>\n<li><strong>oldMap:<\/strong> A map of IDs to the old versions of the sObject records. This map is only available in update and deletes triggers.<\/li>\n<li><strong>size:<\/strong> The total number of records in a trigger invocation, both old and new.<\/li>\n<\/ul>\n<h3>When to use salesforce triggers<\/h3>\n<p>Triggers should be used in those scenarios where point-and-click tools like workflows don&#8217;t work.Workflow is point-and-click which doesn&#8217;t need any coding. When you want to take action (email, task, field update or outbound message) for the same object or from Child to parent object, you can use Workflow rules. Whereas Trigger: is a programmatic approach and event driven (insert, update, merge, delete). We can call it an advanced version of Workflow.<br \/>\nTrigger works across all the objects.<br \/>\nWe can create a new record through triggers which is not possible through workflow. Also, Workflows work only after some actions but triggers work before and after some actions.<\/p>\n<h3>Trigger Syntax<\/h3>\n<p><code>trigger TriggerName on ObjectName (trigger_events) {<br \/>\n   code_block<br \/>\n}<\/code><\/p>\n<h3>Example<\/h3>\n<p><strong>This trigger fires before we insert an account.<\/strong><\/p>\n<ol>\n<li>In the Developer Console, click <strong>File | New | Apex Trigger<\/strong>.<\/li>\n<li>Enter HelloWorldTrigger for the trigger name, and then select Account for the sObject. Click <strong>Submit<\/strong>.<\/li>\n<li>Replace the default code with the following.<\/li>\n<li>trigger <strong>HelloWorldTrigger<\/strong> on <em>Account (before insert) { \tSystem.debug(&#8216;Hello World!&#8217;); }<\/em><\/li>\n<li>To save, press <strong>Ctrl+S<\/strong>.<\/li>\n<li>To test the trigger, create an account.\n<ol>\n<li>Click <strong>Debug | Open Execute Anonymous Window.<\/strong><\/li>\n<li>In the new window, add the following and then click Execute.<\/li>\n<li><em>Account a = new Account(Name=&#8217;Test Trigger&#8217;); insert a;<\/em><\/li>\n<\/ol>\n<\/li>\n<li>In the debug log, find the <em style=\"color:red;\">Hello World!<\/em> statement. The log also shows that the trigger has been executed.<\/li>\n<\/ol>\n<p><small><em>The English language is the most widely used language as a medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an <a href=\"https:\/\/www.studysection.com\/english-language-advanced-diploma\">English Certification Exam<\/a> that tests English language proficiency in English grammar, reading, and writing.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A trigger is an Apex script that executes before or after data manipulation language (DML) events occur. Apex triggers enable<\/p>\n","protected":false},"author":1,"featured_media":5318,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[41,43],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Salesforce Triggers - StudySection Blog<\/title>\n<meta name=\"description\" content=\"Apex triggers enable us to perform custom actions before or after events to record in Salesforce, such as insertions, updates, or deletions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/salesforce-triggers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Salesforce Triggers - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"Apex triggers enable us to perform custom actions before or after events to record in Salesforce, such as insertions, updates, or deletions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/salesforce-triggers\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-23T04:33:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-23T05:15:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/11\/Triggers.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/salesforce-triggers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/salesforce-triggers\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Salesforce Triggers\",\"datePublished\":\"2021-11-23T04:33:32+00:00\",\"dateModified\":\"2021-11-23T05:15:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/salesforce-triggers\/\"},\"wordCount\":765,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"salesforce\",\"triggers\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/salesforce-triggers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/salesforce-triggers\/\",\"url\":\"https:\/\/studysection.com\/blog\/salesforce-triggers\/\",\"name\":\"Salesforce Triggers - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2021-11-23T04:33:32+00:00\",\"dateModified\":\"2021-11-23T05:15:14+00:00\",\"description\":\"Apex triggers enable us to perform custom actions before or after events to record in Salesforce, such as insertions, updates, or deletions.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/salesforce-triggers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/salesforce-triggers\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/salesforce-triggers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Salesforce Triggers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Salesforce Triggers - StudySection Blog","description":"Apex triggers enable us to perform custom actions before or after events to record in Salesforce, such as insertions, updates, or deletions.","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:\/\/studysection.com\/blog\/salesforce-triggers\/","og_locale":"en_US","og_type":"article","og_title":"Salesforce Triggers - StudySection Blog","og_description":"Apex triggers enable us to perform custom actions before or after events to record in Salesforce, such as insertions, updates, or deletions.","og_url":"https:\/\/studysection.com\/blog\/salesforce-triggers\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2021-11-23T04:33:32+00:00","article_modified_time":"2021-11-23T05:15:14+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/11\/Triggers.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/salesforce-triggers\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/salesforce-triggers\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Salesforce Triggers","datePublished":"2021-11-23T04:33:32+00:00","dateModified":"2021-11-23T05:15:14+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/salesforce-triggers\/"},"wordCount":765,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["salesforce","triggers"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/salesforce-triggers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/salesforce-triggers\/","url":"https:\/\/studysection.com\/blog\/salesforce-triggers\/","name":"Salesforce Triggers - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2021-11-23T04:33:32+00:00","dateModified":"2021-11-23T05:15:14+00:00","description":"Apex triggers enable us to perform custom actions before or after events to record in Salesforce, such as insertions, updates, or deletions.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/salesforce-triggers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/salesforce-triggers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/salesforce-triggers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Salesforce Triggers"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":220,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5317"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=5317"}],"version-history":[{"count":4,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5317\/revisions"}],"predecessor-version":[{"id":5321,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5317\/revisions\/5321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5318"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}