+ Start a Discussion
PRADEEP YADAV 3PRADEEP YADAV 3 

TriggerBased

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 -<Account Name>’
Best Answer chosen by PRADEEP YADAV 3
AnudeepAnudeep (Salesforce Developers) 
Hi Pradeep,
 
trigger AddOpportunity on Account(after insert, after update) {
 
    List<Opportunity> oppList = new List<Opportunity>();
    
    // Get the related opportunities for the accounts in the trigger
    Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
        [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);
    
    // Add an opportunity for each account if it doesn't already have one.
    // Iterate through each account. 
    for(Account acc : Trigger.New) {
                 
        // Check if the account already has a related opportunity.      
       if (acctsWithOpps.get(acc.Id).Opportunities.size() == 0) {
             
            // If it doesn't, add a opportunity
            oppList.add(new Opportunity(Name= 'First Opportunity - '+acct.Name,
                                       StageName='Prospecting',                 //set to Prospecting
                                       CloseDate=System.today().addMonths(1),   //set date to one month
                                       AccountId=acc.Id));
        }           
    }

   if (oppList.size() > 0) {
        insert oppList;
   }
}

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Pradeep,
 
trigger AddOpportunity on Account(after insert, after update) {
 
    List<Opportunity> oppList = new List<Opportunity>();
    
    // Get the related opportunities for the accounts in the trigger
    Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
        [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);
    
    // Add an opportunity for each account if it doesn't already have one.
    // Iterate through each account. 
    for(Account acc : Trigger.New) {
                 
        // Check if the account already has a related opportunity.      
       if (acctsWithOpps.get(acc.Id).Opportunities.size() == 0) {
             
            // If it doesn't, add a opportunity
            oppList.add(new Opportunity(Name= 'First Opportunity - '+acct.Name,
                                       StageName='Prospecting',                 //set to Prospecting
                                       CloseDate=System.today().addMonths(1),   //set date to one month
                                       AccountId=acc.Id));
        }           
    }

   if (oppList.size() > 0) {
        insert oppList;
   }
}

Anudeep
This was selected as the best answer
ayu sharma devayu sharma dev
Hello Pradeep Try below code:
 
trigger Trigger_Account on Account (after update, after insert) {
    List<Opportunity> oppToInsert = new List<Opportunity>();
    for( Account acct : [ SELECT Id, Name, ( SELECT Id FROM Opportunities ) FROM Account WHERE Id IN: Trigger.New ] ){
        if( acct.Opportunities.size() == 0 ){
            Opportunity opp = new Opportunity(
                    Name = 'First Opportunity - '+acct.Name,
                    CloseDate = System.today(),
                    StageName = 'Give your stageName here.',
                    AccountId = acct.Id
                );
            oppToInsert.add( opp );
        }
    }
    if( oppToInsert.size() > 0 && ){
        insert oppToInsert;
    }
}

Regards 
Ayush Sharma
PRADEEP YADAV 3PRADEEP YADAV 3
Thanks ayu
PRADEEP YADAV 3PRADEEP YADAV 3
Thanks Anudeep