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 

Inserting bulk related records

Can any one point the mistakes in my code. I'm looking to insert an account record and its related 20 opportunity records and 5 related case records to that account. 

public class AccountHelper {
    
    public static void AccountRecordHelper() {
        
        List<Opportunity> lstopp = New List <opportunity> ();
        List<Case> CaseNew = New List <Case> ();
       
        Account Acc = New Account();
        Acc.Name = 'Gen Construction';
        Acc.Industry= 'Contruction';
        insert acc;
        If (acc.id != Null) {
            
          
            
            for (Integer C = 1; C<=20; C++) {
                Opportunity Opp = New Opportunity ();
                Opp.Name = 'Dean Constructions' + C;
                opp.CloseDate = system.today ();
                opp.StageName = 'Prospecting';
                opp.AccountId = acc.Id;
                lstopp.add(opp);
                insert lstopp;
 
            }
            
            Case Cs = New Case();
            for (integer i = 1; i<=10; i++) {
                
                Cs.Status = 'New';
                Cs.Origin = 'Phone';
                Cs.AccountId = acc.Id;
                casenew.add(cs);
                insert casenew;

            }
            
            
            
            System.debug (lstopp);
            system.debug(casenew);
            system.debug(acc.id);
            
        } 
    }

}
Hara SahooHara Sahoo
Pls avoid DMLs inside the loop as a best practice. rest all looks good