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
sonali  vermasonali verma 

trigger code

Hi
I need help in writing a trigger for the below apex class.
when I create a new account, an opportunity record is created.


Public class OpporInsertAfterAccount
{
   public void Display(List<Account> accounts)
   {
         List<Opportunity> listOpportunities = new List<Opportunity>();
         for(Account oAccount:accounts)
        {
             Opportunity oOpportunity = new Opportunity();
             oOpportunity.Name = oAccount.Name;
             oOpportunity.AccountId = oAccount.Id;
             oOpportunity.StageName = 'Qualification';
             oOpportunity.CloseDate = System.today() + 30; //Closes 30 days from today
             listOpportunities.add(oOpportunity);
        }
    if (listOpportunities.isEmpty() == false)
    {
        Database.insert(listOpportunities);
    }
   }  
}

Thanks
sonali​​​
Best Answer chosen by sonali verma
Arunkumar RArunkumar R
Hi,

1. Write trigger in Account Object.
2. Use the below code and pass the Trigger.new value to the class that you have created.

trigger AccountTrigger on Account(after insert)
{
     OpporInsertAfterAccount oa  = new OpporInsertAfterAccount();
     oa.Display(Trigger.new);
}