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
Khaja BasheerKhaja Basheer 

related records insert for loop

Hi all. Im trying to insert some account records and also its related case and opportunity records. I wrote the below code but it is throwing an error as ' Expecting '}' but was: '<EOF>' 
can any one point the mistake in the code please ?
 
#Apex
Accounnt acc = new Account();
acc.name = 'Samurai'; 
acc.employees = 20; 
insert acc; 
 
if (acc.id != null){
 system.debug(acc.id);    
  for (integer counter = 1 ; counter <= 5; counter++) { 
  case cs = new case ();
  cs.origin = 'Phone'; 
  cs.status = 'high';
  cs.accountid = cs.id;
   insert cs; 
 
   if ( cs.id !=null){  
    system.debug (cs.id); 
    for (integer counter = 1; counter <=5; counter++){
    Opportunity opp = new Opportunity ();  
    opp.Name = 'Salman'; 
    opp.closedate = system.today();
     opp.stage = 'prospecting';  
      opp.accountid=opp.id;  
       insert opp;  
 
        if (opp.id!=null){    
         system.debug(opp.id);   
             }     
        }     
    }   
  }     
Best Answer chosen by Khaja Basheer
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please use this standard code.

Accounnt acc = new Account();
acc.name = 'Samurai'; 
acc.employees = 20; 
insert acc;

List<Case> caseList=new List<Case>();
List<Opportunity> OpportunityList=new List<Opportunity>();

if (acc.id != null){
 system.debug(acc.id);    
  for (integer counter = 1 ; counter <= 5; counter++) { 
  case cs = new case ();
  cs.origin = 'Phone'; 
  cs.status = 'high';
  cs.accountid = cs.id;
  caseList.add(cs);
    }
	insert caseList;
	
	for(Integer i=0;i<5;i++){
	if(caseList[i].Id !=null){
	Opportunity opp = new Opportunity ();  
    opp.Name = 'Salman'; 
    opp.closedate = system.today();
     opp.stage = 'prospecting';  
      opp.accountid=acc.id;
	  OpportunityList.add(opp);
	}
	}
	insert OpportunityList;
}

Please mark it as the Best Answer so that other people would take references from it.

Thank You

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Basheer,

Please change the code as below. You should add '}' at the end and also you are assiging case id to accountid which is wrong. I have modified the code as below and it worked.
 
Account acc = new Account();
acc.name = 'Samurai'; 
//acc.employees = 20; 
insert acc; 
 
if (acc.id != null){
 system.debug(acc.id);    
  for (integer counter = 1 ; counter <= 5; counter++) { 
  case cs = new case ();
  cs.origin = 'Phone'; 
  cs.status = 'high';
  cs.accountid = acc.id;
   insert cs; 
 
   if ( cs.id !=null){  
    system.debug (cs.id); 
    for (integer counter1 = 1; counter1 <=5; counter1++){
    Opportunity opp = new Opportunity ();  
    opp.Name = 'Salman'; 
    opp.closedate = system.today();
     opp.StageName  = 'prospecting';  
      opp.accountid=acc.id;  
       insert opp;  
 
        if (opp.id!=null){    
         system.debug(opp.id);   
             }     
        }     
    }   
  }     }

If this solution helps, Please mark it as best answer.

Thanks,
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please use this standard code.

Accounnt acc = new Account();
acc.name = 'Samurai'; 
acc.employees = 20; 
insert acc;

List<Case> caseList=new List<Case>();
List<Opportunity> OpportunityList=new List<Opportunity>();

if (acc.id != null){
 system.debug(acc.id);    
  for (integer counter = 1 ; counter <= 5; counter++) { 
  case cs = new case ();
  cs.origin = 'Phone'; 
  cs.status = 'high';
  cs.accountid = cs.id;
  caseList.add(cs);
    }
	insert caseList;
	
	for(Integer i=0;i<5;i++){
	if(caseList[i].Id !=null){
	Opportunity opp = new Opportunity ();  
    opp.Name = 'Salman'; 
    opp.closedate = system.today();
     opp.stage = 'prospecting';  
      opp.accountid=acc.id;
	  OpportunityList.add(opp);
	}
	}
	insert OpportunityList;
}

Please mark it as the Best Answer so that other people would take references from it.

Thank You

This was selected as the best answer
mukesh guptamukesh gupta
Hi Khaja,

Please use below code:-
 
Accounnt acc = new Account();
acc.name = 'Samurai'; 
acc.employees = 20; 
insert acc; 
 
List<Case> csList = new List<Case>(); 
List<Opportunity> oppList = new List<Opportunity>(); 
if (acc.id != null){
 system.debug(acc.id);    
  for (integer counter = 1 ; counter <= 5; counter++) { 
  case cs = new case ();
  cs.origin = 'Phone'; 
  cs.status = 'high';
  cs.accountid = cs.id;
   insert cs; 
   csList.add(cs);
 }
 }
 if(csList.size() > 0)
	insert csList;
 
 if(csList.size() > 0){
    for (integer counter = 1; counter <=5; counter++){
	if(csList[i].Id != null){
    Opportunity opp = new Opportunity ();  
    opp.Name = 'Salman'; 
    opp.closedate = system.today();
     opp.stage = 'prospecting';  
      opp.accountid=opp.id; 
		oppList.add(opp)	  
           
        }
	}		
    
	}
	if(oppList.size() > 0)
	insert oppList;

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Khaja BasheerKhaja Basheer
Thanks all. I have just started learning and I haven't yet learned about lists. Thanks Sai tripathi and Mukesh