You need to sign in to do that
Don't have an account?

Writing a trigger that creates multiple opportunities when an account is created
trigger OpportunitiesCreateonAccount on Account (after insert) { for (Account acc :Trigger.new) { for (Integer i = i < 11 (); i++) { if (acc.NumberofEmployees > 99) { List<Opportunity> newOpportunity = new List<Opportunity>(); Opportunity opp = new Opportunity(); opp.Name = acc.Name + i; opp.AccountId = acc.Id; opp.StageName = 'Prospecting'; opp.CloseDate = Date.today(); newOpportunity.add(opp); insert newOpportunity; } } } }
I want to write a trigger that creates 10 opportunity records when an account with employees greater than 99 is created. I keep getting problem on this code. Any help will be appreciated.
Greetings to you!
You are not using the correct syntax for "for" loop: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for.htm
The syntax is:
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
All Answers
You are close, just some of your logic is in the wrong order.
Basically, if the number of employees greater that 99 is the first check which should fire the loop.
Declare the list at the start so that you can use it across the entire trigger loop and place the insert outside the loop. That will reduce the number of DML (think database updates/writes).
Regards
Andrew
note: the code was written free hand - no compile checks.
for (integer i = i <10(); i++) {
Greetings to you!
You are not using the correct syntax for "for" loop: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for.htm
The syntax is:
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas