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
aquelleraqueller 

Appex Trigger

I am trying to write a trigger that will create a new opportunity when one is closed.

The code that I use is as follows:

 

trigger Recurring on Opportunity (after update)
{
    if (Opportunity.StageName == 'Closed Won')
    {
        Opportunity newOpp = new Opportunity (
                                    Account = 'TEST',
                                    StageName = 'Prospecting',
                                    Type = 'Existing Business');
        insert newOpp;
    }
}

I get two errors, one related to the IF statement and the other

related to setting the account.

 

Can someone suggest what are my mistakes and how I can correct them?

 

       Thx

Best Answer chosen by Admin (Salesforce Developers) 
tom_patrostom_patros

"Opportunity" is meaningless inside the trigger. You need to use the Trigger.new context.

 

Also, you can't set the Account to an Account Name, it needs to be an SFID.

 

Finally, you need to make the whole thing bulk-friendly.

 

Something like this (note: not tested):

 

trigger Recurring on Opportunity(after update) {

   List<Opportunity> newOpps = new List<Opportunity>();

   for(Opportunity o : Trigger.new) {
      if(o.StageName == 'Closed Won') {
         Opportunity newOpp = new Opportunity(
            AccountId = 'SOME_SFID',
            StageName = 'Prospecting',
            Type = 'Existing Business'
         );
         newOpps.add(newOpp);
      }
   }

   insert newOpps;

}

 

All Answers

tom_patrostom_patros

"Opportunity" is meaningless inside the trigger. You need to use the Trigger.new context.

 

Also, you can't set the Account to an Account Name, it needs to be an SFID.

 

Finally, you need to make the whole thing bulk-friendly.

 

Something like this (note: not tested):

 

trigger Recurring on Opportunity(after update) {

   List<Opportunity> newOpps = new List<Opportunity>();

   for(Opportunity o : Trigger.new) {
      if(o.StageName == 'Closed Won') {
         Opportunity newOpp = new Opportunity(
            AccountId = 'SOME_SFID',
            StageName = 'Prospecting',
            Type = 'Existing Business'
         );
         newOpps.add(newOpp);
      }
   }

   insert newOpps;

}

 

This was selected as the best answer
raseshtcsraseshtcs

The error for the Account Id was because you can't create an opportunity unless you specify an Account for the same(As Tom Suggested) 

trigger Recurring on Opportunity(after update) {

   List<Opportunity> newOpps = new List<Opportunity>();

   for(Opportunity o : Trigger.new) {
      if(o.StageName == 'Closed Won') {
         Opportunity newOpp = new Opportunity(
            AccountId = 'SOME_SFID',/*You can use "o.AccountId" instead of "SOME_SFID"if you want to attach the new opportunity to the Account to which the old opportunity belonged to*/
            StageName = 'Prospecting',
            Type = 'Existing Business'
         );
         newOpps.add(newOpp);
      }
   }

   insert newOpps;
 }
aquelleraqueller

Thank you.

aquelleraqueller

Thank you

aquelleraqueller

Hi,

 

I have one more question:

 

I followed your advise and wanted to search the associated account, so that I can reference

the account number. The code is as follows:

 

trigger Recurring on Opportunity(after update)
{
   // Create a list of Opportunities, newOpps, to make it bulk fiendly
   List<Opportunity> newOpps = new List<Opportunity>();

   for(Opportunity o : Trigger.new)
   {
      if(o.StageName == 'Closed Won')
      {
         Opportunity newOpp = new Opportunity(
                                        AccountId = o.AccountId,
                                        Amount = o.Amount + 2000,
                                        Name = o.Name,
                                        CloseDate = date.today()+ 90,
                                        StageName = 'Prospecting',
                                        Type = 'Existing Business'
                                             );

     Account myAcct = [select AccountNumber from account where id = newOpp.AccountId];
     newOpp.Description = myAcct.AccountNumber;


         newOpps.add(newOpp);
      }
   }

   insert newOpps;
}

 I get 'Compile Error: unexpected token: 'newOpp.AccountId' ' on the query.

 

Could you tell me how I should construct the query?

tom_patrostom_patros

You're very close - you need to add a colon before any variables referenced in a soql query to bind them correctly. Try:

 

Account myAcct = [select AccountNumber from account where id = :newOpp.AccountId];

 Note the colon before "newOpp.AccountId"

 

That said, your update has made the trigger bulk-unfriendly, because you are executing a SOQL within a loop iterating over 1+ Opportunities. Probably not an issue normally, but if you were to import 200 Opportunites, this trigger would exceed the SOQL call limit. Try to do you SOQL queries outside of loops to prevent this.

aquelleraqueller

Thank you very much for you quick(!!!) response.