You need to sign in to do that
Don't have an account?
aqueller
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
"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):
All Answers
"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):
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)
Thank you.
Thank you
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:
I get 'Compile Error: unexpected token: 'newOpp.AccountId' ' on the query.
Could you tell me how I should construct the query?
You're very close - you need to add a colon before any variables referenced in a soql query to bind them correctly. Try:
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.
Thank you very much for you quick(!!!) response.