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
Vishnuvardhan Reddy 14Vishnuvardhan Reddy 14 

Need to create 2 different stage opportunities for an Account creation

Hi All,
Below is my code snippet but it is not working..


trigger Acc_Opty_Creation on Account (after insert) {
    List <Account> accList = Trigger.New;
  
    User u= [Select id from user where alias ='integ'];
    List<Opportunity> opty= New List<Opportunity>();
    List<Opportunity> opty2= New List <Opportunity>();
    
     
    for (Account a : accList){
        Opportunity op = new Opportunity();
        op.Name= a.Name+'_Opty';
        op.CloseDate=System.today()+15;
        op.StageName='Prospecting';
        op.AccountId= a.Id;
        op.OwnerId=u.Id;
        opty.add(op); 
    }
    insert opty;
    
    for (Account a : accList){
        Opportunity Op2 = new Opportunity();
        op2.Name= a.Name+'_Opty';
        op2.CloseDate=System.today()+20;
        op2.StageName='Needs Analysis';
        op2.AccountId= a.Id;
        op2.OwnerId=u.Id;
        opty2.add(op2); 
   }
   insert opty2;
}
CharuDuttCharuDutt
 Hii Vishnuvardhan
 try this code and please let me know if it helps and mark it as best answer..


   List<Opportunity> optyList=new List<Opportunity>();
           
    
    for(Account a:Trigger.new)
    {
        integer i = 0;
        Opportunity opp = new Opportunity();
        opp.AccountId=a.id;
        opp.Name='Created from account'+ i++;
        opp.StageName='Prospecting';
        opp.LeadSource = 'Web';
        opp.CloseDate=Date.toDay();
        optyList.add(opp);
        
         Opportunity opp1 = new Opportunity();
        opp1.AccountId=a.id;
        opp1.Name='Created from account' +i++;
        opp1.StageName='Closed Won';
        opp1.LeadSource = 'Other';
        opp1.CloseDate=Date.toDay();
        optyList.add(opp1);

    }
    insert optyList;