{"id":2162,"date":"2020-02-14T08:30:55","date_gmt":"2020-02-14T08:30:55","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=2162"},"modified":"2020-02-24T12:59:05","modified_gmt":"2020-02-24T12:59:05","slug":"apex-trigger-best-practices","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/","title":{"rendered":"Apex Trigger Best Practices"},"content":{"rendered":"<ol>\n<li>\n<h2>Single trigger for each object<\/h2>\n<p>An object is supposed to have only one Apex Trigger. If we create multiple Triggers for a single object, we will not be able to connect the flow of all the triggers and expect it to execute synchronously. It might happen that the apex logic conflicts with the other trigger belonging to the same object.<\/li>\n<li>\n<h3>Triggers with no logic<\/h3>\n<p>There should not be any logic in the Apex trigger. It is true that we can have functions there, but a developer will not be able to write test cases for those functions as those can not be exposed for test purposes. There is an option to update or insert a record to fire a trigger, but this is not considered a good practice. Additionally, these logics can not be used at other places if we wish to. As a solution, create a handler class via which you can access all the functional methods.<\/li>\n<li>\n<h3>Handler methods with defined context.<\/h3>\n<p>There should be different functions in the handler class for different contexts. For example, a function named onBeforeInsert(). This function should only be called when Trigger.isInsert AND Trigger.isBefore is true.<\/li>\n<li>\n<h3>Code bulkification<\/h3>\n<p>Apex code should always be written so that it can handle bulk data at a time. The main idea behind this is to have an optimized code so that the feature works efficiently. Moreover, to avoid governor limits.<\/li>\n<li>\n<h3>No SOQL Queries or DML statements inside FOR Loop<\/h3>\n<p>There should never be a SOQL query or any DML operation done inside a FOR loop. For one transaction\/session, an apex request can use 100 queries. If the session tries to use beyond this limit, the process will stop immediately thus throwing a run-time exception. In case the data loader is used to push records (say more than 100) for that particular object, and the SOQL query is used inside a loop, it will throw an error.<\/li>\n<li>\n<h3>Use Collections, Streamlined Queries, and Efficient For Loops<\/h3>\n<p>Always use apex collections (List or Map) to hold data retrieved from the query. Using collections and organized queries will altogether contribute to efficient code and help in running the process within the apex governor limits. Do not iterate the same collection multiple times if it can be iterated once for a logic using IF-ELSE statements.<\/li>\n<li>\n<h3>Retrieving bulk data in SOQL<\/h3>\n<p>Per governor limits, the number of records that can be returned by SOQL queries in one request is 50,000. If the query has to return records more than 50,000, then use &#8220;SOQL query for loop&#8221;. This helps in processing multiple batches of records. It calls \u201cquery\u201c and \u201cqueryMore\u201d asynchronous functions internally.<\/li>\n<li>\n<h3>Leverage @future annotation when required<\/h3>\n<p>The future method seems to be a good option when we face governor limits issues because the asynchronous function has its own independent set of higher governor limits. It is very important to use future annotated functions to handle bulk data. The reason is, it also has its own limitations at one point. We can have only 10 future annotated functions in a single transaction.<\/li>\n<li>\n<h3>Never use Hard-Coded IDs<\/h3>\n<p>Whenever we deploy apex components to different <a href=\"https:\/\/studysection.com\/blog\/salesforce-apex-programming\/\">salesforce organizations<\/a>, it will throw an error because that Id may belong to another object in deployed org. Also, it may happen that the Id we used in the code does not even exist there. It will break the feature. Also, it is possible that while testing you may not find the issue because the Id does exist in the deployed org.<\/li>\n<\/ol>\n<p><small><em>jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The <a href=\"https:\/\/www.studysection.com\/jquery-3.x-foundation\">jQuery Certification exam<\/a> by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Single trigger for each object An object is supposed to have only one Apex Trigger. If we create multiple Triggers<\/p>\n","protected":false},"author":1,"featured_media":2163,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[42,363],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog About Apex Trigger Best Practices<\/title>\n<meta name=\"description\" content=\"If we create multiple Apex Trigger for a single object, we will not be able to connect the flow of all the triggers and expect it to execute synchronously\" \/>\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\/apex-trigger-best-practices\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog About Apex Trigger Best Practices\" \/>\n<meta property=\"og:description\" content=\"If we create multiple Apex Trigger for a single object, we will not be able to connect the flow of all the triggers and expect it to execute synchronously\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/\" \/>\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=\"2020-02-14T08:30:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-02-24T12:59:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/02\/apex.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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Apex Trigger Best Practices\",\"datePublished\":\"2020-02-14T08:30:55+00:00\",\"dateModified\":\"2020-02-24T12:59:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/\"},\"wordCount\":628,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"apex\",\"Trigger\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/\",\"url\":\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/\",\"name\":\"StudySection Blog About Apex Trigger Best Practices\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-02-14T08:30:55+00:00\",\"dateModified\":\"2020-02-24T12:59:05+00:00\",\"description\":\"If we create multiple Apex Trigger for a single object, we will not be able to connect the flow of all the triggers and expect it to execute synchronously\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Trigger Best Practices\"}]},{\"@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":"StudySection Blog About Apex Trigger Best Practices","description":"If we create multiple Apex Trigger for a single object, we will not be able to connect the flow of all the triggers and expect it to execute synchronously","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\/apex-trigger-best-practices\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog About Apex Trigger Best Practices","og_description":"If we create multiple Apex Trigger for a single object, we will not be able to connect the flow of all the triggers and expect it to execute synchronously","og_url":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-02-14T08:30:55+00:00","article_modified_time":"2020-02-24T12:59:05+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/02\/apex.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Apex Trigger Best Practices","datePublished":"2020-02-14T08:30:55+00:00","dateModified":"2020-02-24T12:59:05+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/"},"wordCount":628,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["apex","Trigger"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/","url":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/","name":"StudySection Blog About Apex Trigger Best Practices","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-02-14T08:30:55+00:00","dateModified":"2020-02-24T12:59:05+00:00","description":"If we create multiple Apex Trigger for a single object, we will not be able to connect the flow of all the triggers and expect it to execute synchronously","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/apex-trigger-best-practices\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Trigger Best Practices"}]},{"@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":829,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/2162"}],"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=2162"}],"version-history":[{"count":6,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/2162\/revisions"}],"predecessor-version":[{"id":2228,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/2162\/revisions\/2228"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/2163"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=2162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=2162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=2162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}