function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PRADEEP YADAV 5PRADEEP YADAV 5 

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
PRADEEP YADAV 5PRADEEP YADAV 5
In bulkified Trigger How to doing Insert and update trigger at same time
Shawn Reichner 33Shawn Reichner 33
Use Upsert instead of Insert and that should cover both scenarios of insert and update.
PRADEEP YADAV 5PRADEEP YADAV 5
Thanks Shawn Reicher
 
PRADEEP YADAV 5PRADEEP YADAV 5
Shawn Reicher Please Given one Simple Example of Helper Class Based on This Scerio Based  Question