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
Raj ChatterjeeRaj Chatterjee 

Expecting '}' but was: 'for'

Can anyone tell why getting this error.Here is my code
public class insertclass 
{
   public static void insertMethod(string LastNameOfAccount)
   {
       List<Account> acclist = New List<Account>();      
           account acc = new account();
           acc.name = LastNameOfAccount;
           acclist.add(acc);         
        system.debug('.....acclist'+acclist);
         insert acclist;
       List<Account> acc1 = New List<Account>();
       acc1 = [SELECT Name,Id From Account Where name =:LastNameOfAccount];
       system.debug('acc1...'+acc1);
       Id accountId;
       for (Account acc23: acc1)     
       {
           accountId = acc23.Id ;
           system.debug('acc2---'+accountId);
       } 
         List<Contact> conlist = New List<Contact>();
         for(integer i = 0; i<2; i++)
         {           
            Contact con = new contact();
            con.Firstname = 'Test'+ string.valueOf(i);
            con.Lastname = 'Ramesh';
            con.accountId =  accountId ;
            conlist.add(con);       
         }      
         system.debug('.....conlist'+conlist);
         insert conlist;
     }
                    List<opportunity> opplist = New List<opportunity>();
error line >        for(integer i = 0; i<2; i++)
              {           
            opportunity opp = new opportunity();
            opp.Firstname = 'Test'+ string.valueOf(i);
            opp.Lastname = 'Ramesh';
            opp.accountId =  accountId ;
            opplist.add(opp);       
         }      
         system.debug('.....opplist'+opplist);
         insert opplist;
     }
Thanks is advance.
Anthony McDougaldAnthony McDougald
Hello Raj,
Hope that your day is off to an amazing start. Your code has been assessed and fixed. Hope this helps and may God bless you abundantly.
public class insertclass 
{
   public static void insertMethod(string LastNameOfAccount){
       List<Account> acclist = New List<Account>();      
       account acc = new account();
       acc.name = LastNameOfAccount;
       acclist.add(acc);         
       system.debug('.....acclist'+acclist);
       insert acclist;
       List<Account> acc1 = New List<Account>();
       acc1 = [SELECT Name,Id From Account Where name =:LastNameOfAccount];
       system.debug('acc1...'+acc1);
       Id accountId;
       for (Account acc23: acc1){
           accountId = acc23.Id ;
           system.debug('acc2---'+accountId);
       } 
         List<Contact> conlist = New List<Contact>();
         for(integer i = 0; i<2; i++){           
            Contact con = new contact();
            con.Firstname = 'Test'+ string.valueOf(i);
            con.Lastname = 'Ramesh';
            con.accountId =  accountId ;
            conlist.add(con);       
         }      
         system.debug('.....conlist'+conlist);
         insert conlist;
         List<opportunity> opplist = New List<opportunity>();
         for(integer i = 0; i<2; i++){           
            opportunity opp = new opportunity();
            opp.Firstname = 'Test'+ string.valueOf(i);
            opp.Lastname = 'Ramesh';
            opp.accountId =  accountId ;
            opplist.add(opp);       
         }      
         system.debug('.....opplist'+opplist);
         insert opplist;
     }
}


Best Regards,
Anthony McDougald
sachinarorasfsachinarorasf
Hi Raj,

Here is updated code please use the below code: 

public class insertclass 
{
    public static void insertMethod(string LastNameOfAccount)
    {
        List<Account> acclist = New List<Account>();      
        account acc = new account();
        acc.name = LastNameOfAccount;
        acclist.add(acc);         
        system.debug('.....acclist'+acclist);
        insert acclist;
        List<Account> acc1 = New List<Account>();
        acc1 = [SELECT Name,Id From Account Where name =:LastNameOfAccount];
        system.debug('acc1...'+acc1);
        Id accountId;
        for (Account acc23: acc1)     
        {
            accountId = acc23.Id ;
            system.debug('acc2---'+accountId);
        } 
        List<Contact> conlist = New List<Contact>();
        for(integer i = 0; i<2; i++)
        {           
            Contact con = new contact();
            con.Firstname = 'Test'+ string.valueOf(i);
            con.Lastname = 'Ramesh';
            con.accountId =  accountId ;
            conlist.add(con);       
        }      
        system.debug('.....conlist'+conlist);
        insert conlist;
        
        List<opportunity> opplist = New List<opportunity>();
        for(integer i = 0; i<2; i++)
        {           
            opportunity opp = new opportunity();
            opp.name = 'Test'+ string.valueOf(i);
            opp.accountId =  accountId ;
            opplist.add(opp);       
        }      
        system.debug('.....opplist'+opplist);
        insert opplist;
    } /*Put curly braces here for the method*/
}

You had closed curly braces at the wrong place that is why you get the error in code. 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com