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
Porty 23110Porty 23110 

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.
Best Answer chosen by Porty 23110
Khan AnasKhan Anas (Salesforce Developers) 
Hi Porty,

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:
for (init_stmt; exit_condition; increment_stmt) {
    code_block
}

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger OpportunitiesCreate on Account (after insert) {
    
    List<Opportunity> newOpportunity = new List<Opportunity>();
    for (Account acc :Trigger.new) {
        if (acc.NumberofEmployees > 99) {
            for (Integer i=1; i<=10; i++)  {
                
                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 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

Andrew GAndrew G
Hi Porty
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.
 
trigger OpportunitiesCreateonAccount on Account (after insert) {
      List<Opportunity> newOpportunity = new List<Opportunity>();

    for (Account acc :Trigger.new) 
    {
        if (acc.NumberofEmployees > 99) 
        {
            for (Integer i = i <  11 (); i++)  
            {
                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;
}

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.

 
Porty 23110Porty 23110
Hi Andrew. Thank you for explaning some concepts to me. I have made the correction but getting error for my loop logic. Could you please review my code below? All the errors i'm getting is from the loop;

for (integer i = i <10(); i++) {
trigger OpportunitiesCreate on Account (after insert) {
      
    List<Opportunity> newOpportunity = new List<Opportunity>();
    for (Account acc :Trigger.new) {
        if (acc.NumberofEmployees > 99) {
            for (Integer i = i < 10 (); i++)  {
              
            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;
            
            
    }
Khan AnasKhan Anas (Salesforce Developers) 
Hi Porty,

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:
for (init_stmt; exit_condition; increment_stmt) {
    code_block
}

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger OpportunitiesCreate on Account (after insert) {
    
    List<Opportunity> newOpportunity = new List<Opportunity>();
    for (Account acc :Trigger.new) {
        if (acc.NumberofEmployees > 99) {
            for (Integer i=1; i<=10; i++)  {
                
                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 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
This was selected as the best answer
Porty 23110Porty 23110
Thank you for your help @Khan. Can't believe i didn't see i had written 1 as i. Sigh.. Beginner's woes. Thanks again 
Andrew GAndrew G
All good Porty.  I missed that one too  ;-)