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;
}
}
}
}