Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":893,"date":"2017-08-08T10:29:37","date_gmt":"2017-08-08T10:29:37","guid":{"rendered":"http:\/\/www.salesforcenextgen.com\/?p=893"},"modified":"2020-12-28T19:43:34","modified_gmt":"2020-12-28T19:43:34","slug":"bulkifying-an-apex-trigger","status":"publish","type":"post","link":"https:\/\/salesforcenextgen.com\/bulkifying-an-apex-trigger\/","title":{"rendered":"Design Patterns for Bulkifying an Apex Trigger"},"content":{"rendered":"

Design Patterns for bulkifying an Apex Trigger<\/strong><\/span><\/p>\n

As Salesforce is built on the multi-tenant architecture, it has multiple platform limits which govern the uses of resources at one particular transaction. As the Apex trigger can operate on multiple records at the same time, it can easily breach the governor limits and therefore it is suggested that Apex trigger should be bulkified before it is moved to production.<\/p>\n

Benefits of bulkified Trigger are many for e.g.:<\/p>\n

    \n
  1. Better performance<\/li>\n
  2. Consume fewer resources and time over the server<\/li>\n
  3. It rarely breach the governor limits<\/li>\n<\/ol>\n

    To bulkify trigger means to work on collections of sObject instead of individual records. Bulk design concept works on all sObject in the context variable. Normally trigger executes on one record at a time if the origin of the execution was through the user interface, but if the origin was a bulk API or DML then the trigger executes on the whole set rather than one record at time, therefore we should keep in mind that the trigger may perform on the collection of sObject and it is a best practice to bulkify the trigger.<\/p>\n

    Let have a look in the below example:<\/p>\n

    Trigger FirstBulktrigger on Account(before insert){<\/p>\n

    For(account test : Trigger.New){<\/p>\n

    Test.description = \u2018Bulk Trigger was used\u2019;<\/p>\n

    }<\/p>\n

    }<\/p>\n

    In the above Apex trigger, we traverse through each and every account record available in the Trigger.New context variable and set the description of each account to \u2018Bulk Trigger was used\u2019.<\/p>\n

    Performing SOQL in a bulkified Trigger<\/span><\/h1>\n

    SOQL is used in Trigger for so many reasons and one reason could be to fetch the related records. To fetch the related records of all the available records in the Trigger\u2019s context variable, we are likely to hit the platform limits which 100SOQL per synchronous transaction.<\/p>\n

    The best practices suggest the following code pattern:<\/p>\n

      \n
    1. Avoid writing SOQL in for loops in a Trigger.<\/li>\n
    2. Always fetch the required amount of records in the Query by using where filter clause.<\/li>\n
    3. We should try to use inner query wherever possible.<\/li>\n<\/ol>\n

      Let\u2019s have a look at the below example:<\/p>\n

      Trigger BulkSOQlExampleTrigger on Account( before insert){<\/p>\n

      List<account> accountListWithOpp = [select Id, Name (select name from contacts)from Account where Id IN : Trigger.New \u00a0];<\/p>\n

      For(Account acc : accountListWithOpp){<\/p>\n

      Contact[] relatedContacts = acc.contacts;<\/p>\n

      }<\/p>\n

      }<\/p>\n

      We can perform a bulk DML as well in a similar way:<\/p>\n

      Trigger BulkSOQlExampleTrigger on Account( before insert){<\/p>\n

      List<account> accountListWithOpp = [select Id, Name (select name from contacts)from Account where Id IN : Trigger.New \u00a0];<\/p>\n

      For(Account acc : accountListWithOpp){<\/p>\n

      Contact[] relatedContacts = acc.contacts;<\/p>\n

      }<\/p>\n

      For(Contact con : relatedContacts){<\/p>\n

      Con.lastname = \u2018Bulk DML\u2019;<\/p>\n

      }<\/p>\n

      }<\/p>\n

      And that\u2019s how we bulkify a Trigger.<\/p>\n

      Want to know about interview questions related to Apex Trigger then click on the below<\/p>\n

      Interview Questions Apex Trigger<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"

      Design Patterns for bulkifying an Apex Trigger As Salesforce is built on the multi-tenant architecture, it has multiple platform limits which govern the uses of resources at one particular transaction. As the Apex trigger can operate on multiple records at the same time, it can easily breach the governor limits and therefore it is suggested …
      Continue reading Design Patterns for Bulkifying an Apex Trigger<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[40,79,61,74,45,75,38,42,55,51,52],"_links":{"self":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/893"}],"collection":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/comments?post=893"}],"version-history":[{"count":5,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/893\/revisions"}],"predecessor-version":[{"id":2207,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/893\/revisions\/2207"}],"wp:attachment":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media?parent=893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/categories?post=893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/tags?post=893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}