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
Karthik keyanKarthik keyan 

After creating account the oppty not created in trigger

I  written the trigger code that after creating new account the oppty should be created automatically.But the account was created but oppty seems to be not created.Could you please check this code its suitable to create or need to change the code further.
trigger CreateAccountOppty on Account (before insert,before update)
{    List<Opportunity> acc = new List<Opportunity>(); 
       for(Account acc1:Trigger.new)  
    {    
          if(acc1.Rating =='Hot' )   
                {       
                     Opportunity  oppty = new Opportunity ();  
                     oppty.name= acc1.Name;      
                      oppty.Opportunity_Status__c='Active';    
                      oppty.StageName ='Prospecting';
                       oppty.CloseDate= date.today()+1;  
                         acc.add(oppty);       
                      }          
                      insert acc; 
     }       
  }
Best Answer chosen by Karthik keyan
Arunkumar RArunkumar R
Hi Karthik,

You need need to use after trigger, I modified your code and check it.
trigger CreateAccountOppty on Account (after insert,after update)
{    
	List<Opportunity> acc = new List<Opportunity>(); 
	for(Account acc:Trigger.new)  
	{    
		if(acc.Rating == 'Hot' )   
		{       
		Opportunity oppty = new Opportunity ();  
		oppty.name= acc1.Name;      
		oppty.Opportunity_Status__c='Active';    
		oppty.StageName ='Prospecting';
		oppty.CloseDate= date.today()+1;  
		acc.add(oppty);       
		}          
	}
	insert acc; 
	
}

Please make sure all the mandatory fields in opportunity need to assign the values else opportunity record will not be created. Let me know if still if you face problem.

All Answers

Arunkumar RArunkumar R
Hi Karthik,

You need need to use after trigger, I modified your code and check it.
trigger CreateAccountOppty on Account (after insert,after update)
{    
	List<Opportunity> acc = new List<Opportunity>(); 
	for(Account acc:Trigger.new)  
	{    
		if(acc.Rating == 'Hot' )   
		{       
		Opportunity oppty = new Opportunity ();  
		oppty.name= acc1.Name;      
		oppty.Opportunity_Status__c='Active';    
		oppty.StageName ='Prospecting';
		oppty.CloseDate= date.today()+1;  
		acc.add(oppty);       
		}          
	}
	insert acc; 
	
}

Please make sure all the mandatory fields in opportunity need to assign the values else opportunity record will not be created. Let me know if still if you face problem.

This was selected as the best answer
Karthik keyanKarthik keyan
Hi Arunkumar,

  Thanks for the exact coding ,that created oppty record,but the oppty created is not under newly created account .So i cant find the oppty record in the newly created  account page.How to correlated the account and Oppty to show created oppty under the account.

Thanks,
Kathikeyan