Create Related Opportunity on Account Creation

This trigger creates a new Opportunity with default values whenever an Account is created, automating Opportunity setup for new Accounts.

For similar examples, visit this YouTube playlist.

Solution:

trigger AccountTrigger on Account (after insert) {

              if(Trigger.isInsert){ if(Trigger.isAfter){
                          AccountTriggerHandler.createRelatedOpp(Trigger.New);
               }
        }
}

          public class AccountTriggerHandler {

          public static void createRelatedOpp(List<Account> accList){
                            List<Opportunity> oppList = new List<Opportunity> ();
                              for(Account acc: accList){
                                    Opportunity opp = new Opportunity(); opp.Name =
                                    acc.Name + ‘opp’; opp.AccountId = acc.Id;
                                   opp.StageName = ‘Prospecting’; opp.CloseDate =
                                   System.today(); oppList.add(opp);
               }
                         if(!oppList.isEmpty()){
                              insert oppList;
                       }
               }
         }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)