You need to sign in to do that
Don't have an account?

When an account is inserted or updated,check whether any opportunity is linked to it or not,if not then create one whose name is ‘First Opportunity
trigger Trigger1Problem on Account(after insert, after update)
{
List<Opportunity> opp = new List<Opportunity>();
Map<ID, Account> accountmap = new Map<ID, Account>([Select Id, Name, (Select Id From Opportunities) From Account Where Id In :Trigger.New]);
for(Account a : trigger.new)
{
if(accountmap.get(a.Id).Opportunities.size() == 0)
{
opp.add(new Opportunity(AccountId = a.Id, Name ='First Opportunity'+a.Name, StageName ='prospecting',CloseDate =System.today()));
}
}
if(opp.size()>0)
{
Insert opp;
}
}
Helper Class Which Way Doing
{
List<Opportunity> opp = new List<Opportunity>();
Map<ID, Account> accountmap = new Map<ID, Account>([Select Id, Name, (Select Id From Opportunities) From Account Where Id In :Trigger.New]);
for(Account a : trigger.new)
{
if(accountmap.get(a.Id).Opportunities.size() == 0)
{
opp.add(new Opportunity(AccountId = a.Id, Name ='First Opportunity'+a.Name, StageName ='prospecting',CloseDate =System.today()));
}
}
if(opp.size()>0)
{
Insert opp;
}
}
Helper Class Which Way Doing

In bulkified Trigger How to doing Insert and update trigger at same time

Use Upsert instead of Insert and that should cover both scenarios of insert and update.

Thanks Shawn Reicher

Shawn Reicher Please Given one Simple Example of Helper Class Based on This Scerio Based Question