Insert Bulk Accounts and 5 Contacts to Each Account

Code Snippet to Bulk Insert Accounts and 5 Contacts to Each Account.

Execute the below script in developer console. It will create 5 Accounts records and each Account have 5 Contacts.

For(integer i=0; i<5; i++) { 
	Account acc = new Account(Name='Bulk Account '+i, AccountNumber='919102'+i, Rating='Hot');
	Insert acc;
    For(integer j=0; j<5; j++) { 
        Contact con = new Contact(LastName=acc.Name +' Contact '+j, AccountId=acc.Id);
		Insert con;
    }
}

Leave a Reply