4

I've been learning Apex over the last month and have been practicing building triggers. One of my practice scenarios is to build a trigger that when a new Account is created and it is of type "Prospect" to create a new Opportunity and relate it back to the Account. (I know this can be done with Process Builder, but I want to do it with Apex so I can continue to learn). I wrote the trigger two ways (and they both seem to work), and I wanted to know which way is the "preferred" way to handle bulkification. Version 1 does a loop on the entire Trigger.new and uses an If statement to see if it is of type Prospect, while Version 2 uses a SOQL query upfront to "filter" on the Accounts with Prospect type only, and then I loop on that list. Would love to know which is the best practice. Thanks everyone!

Version 1:

trigger CreateFunOpp on Account (after insert) {

    // List of Opps
    List<Opportunity> newOpps = new List<Opportunity>();

    for (Account acc : Trigger.new) {
        if (acc.Type == 'Prospect') {
            Opportunity opp = new Opportunity();
            opp.Name = acc.Name + ' Opportunity';
            opp.StageName = 'Prospecting';
            opp.CloseDate = Date.today()+30;
            opp.AccountID = acc.Id;
            newOpps.add(opp);
            }

        }
    insert newOpps;
}

Version 2:

trigger CreateFunOpp2 on Account (after insert) {

   List<Account> accountList = [SELECT Id,Name FROM Account WHERE Id IN :Trigger.New AND TYPE='PROSPECT'];

    //List of Opportunities to create
    List<Opportunity> oppList = new List<Opportunity>();

    for (Account acc : accountList) {
            Opportunity opp = new Opportunity();
            opp.Name = acc.Name + ' Opportunity Too';
            opp.StageName = 'Prospecting';
            opp.CloseDate = Date.today()+30;
            opp.AccountID = acc.Id;
            oppList.add(opp);
    }

    insert oppList;

}
6

Version 1 would be the preferred method over the two that you've written. NEVER use a query in a trigger unless it's absolutely necessary.

In this case, you already have everything you need in trigger.new, so there's no need to query the database as you have in Version 2.

4
  • Thank you very much crmprogdev..this helps a lot! Out of curiosity would Version 2 still pass any bulk tests?
    – Mike Fuchs
    Sep 28 '15 at 0:42
  • It would be bulk safe, but it wouldn't work as intended. You didn't query the Name field on Account, so it would throw an error message saying "field wasn't queried" (or something to that effect) when you try to assign a value to opp.Name.
    – crmprogdev
    Sep 28 '15 at 0:51
  • Thanks. I actually realized I forgot to query the Name field right after I pasted it in here!
    – Mike Fuchs
    Sep 28 '15 at 1:19
  • You're welcome Mike. Community etiquette is to help others by marking your question as solved through checking the answer that resolved your issue. If you don't, it leaves the impression to anyone who searches the topic later that your question wasn't answered to your satisfaction. Once you have sufficient reputation, you can also upvote posts and answers.
    – crmprogdev
    Sep 28 '15 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.