User Story
I am a User.
WHEN I create a new Account
THEN an Opportunity will be created for those Accounts that don't have any related Opportunity
Opportunity Name : Account Name + Opportunity
Stage : Prospecting
Close Date : Two Months away from Today
Code
trigger AccountTrigger on Account (after insert, after update) {
List<Opportunity> oppList = new List<Opportunity>();
List<Account> acList =[SELECT Id, Name FROM Account WHERE Id IN: Trigger.new and id NOT IN (SELECT AccountId FROM Opportunity)];
for(Account ac : acList){
oppList.add(new Opportunity (Name = ac.Name + 'Opportunity',
StageName='Prospecting',
CloseDate=System.today().addMonths(2),
AccountId=ac.Id));
}
if(oppList.size()!=0){
insert oppList;
}
}