function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ZayneLoftZayneLoft 

To Bulkify or Not To Bulkify - My first trigger

I've just created my first trigger which fires on opportunities and checks to see if an opportunity was paid for using a gift certificate custom object that we created and if so then goes and debits the apporpriate amount from that gift certificate. Part of this involves a SOQL to create a list of all the gift certificates that currently have a balance greater than 0. As their is a governor limit on the amount of SOQL queries you can do, and as there is such an emphasis on "bulkification" of your code, I initally placed this SOQL query at the beginning of the trigger, outside the for loop that acts on every record so as not to fire mulitple times when bulk uploading opportunities. That being said, I don't plan on ever bulk uploading opportunities. 


Here's the question. Given that most opportunities won't be paid for with a gift certificate and the fact that I have no intention of ever bulk uploading opportunities, would it make more sense to forego the bulkification best practice and only perform the SOQL query after determining whether or not the query needed to be made? 

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

In my experience not bulkifying your triggers comes back to bite you more often than you think.  Its sort of like playing with fire cause you never know how you will use the system in the future.  

 

Just one small example that I can think of that could hurt you, what if a year from now you or someone in your company buys a list of leads from a 3rd party for an expansion effort and you want to upload them, but then you realize that you have logic that is going to automatically convert some of those leads to opps.  I am not saying that this will happen for sure, but that is a fairly simplistic example of how it could bite you in the but later.  Hope that this helps.

All Answers

Jake GmerekJake Gmerek

In my experience not bulkifying your triggers comes back to bite you more often than you think.  Its sort of like playing with fire cause you never know how you will use the system in the future.  

 

Just one small example that I can think of that could hurt you, what if a year from now you or someone in your company buys a list of leads from a 3rd party for an expansion effort and you want to upload them, but then you realize that you have logic that is going to automatically convert some of those leads to opps.  I am not saying that this will happen for sure, but that is a fairly simplistic example of how it could bite you in the but later.  Hope that this helps.

This was selected as the best answer
crop1645crop1645

Jake is so right -- always bulkify. Here are other reasons why:

 

1 - Data Loader repair to your SObject Opportunity.  You won't be able to do the repair if your trigger isn't bulkified

2 - Some API access in the future that uses bulk operations (as recommended by the developer doc)

3 - Some other after trigger on SObject X that updates/inserts Opportunities (in bulk) - the after Trigger on X is bulkified but if the Oppo trigger isn't .... 

 

If you ever think you will touch Apex code in the future, it is inevitable that you must bulkify.  It isn't hard and there are tons of examples. Drink the cool-aid!

ZayneLoftZayneLoft

To the both of you, thanks for your recommendations. It sounds like there's little reason not to bulkify and lots of reason to do so.