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
Veerchand ChintakulaVeerchand Chintakula 

How to write an apex class to insert 1 account and 10 contacts records and how to associate each contact to created account?

NagendraNagendra (Salesforce Developers) 
Hi Veerchand,

May I suggest you please find the sample code below for accounts, and tweak it as per the requirement.

Visual Force Page:
<apex:page Controller="AddmultipleAccountsController">
<apex:form >

    <apex:pageBlock >
    <apex:outputText value="Accounts Saved" rendered="{!msgForUser}" style="color:red;"/>
        <apex:pageBlockTable value="{!listAccount}" var="acc">
            <apex:column headerValue="Account Name">
                <apex:inputField value="{!acc.Name}"/>
            </apex:column>
            <apex:column headerValue="Account Number">
                <apex:inputField value="{!acc.AccountNumber}"/>
            </apex:column>
            <apex:column headerValue="Account Type">
                <apex:inputField value="{!acc.Type}"/>
            </apex:column>
            <apex:column headerValue="Industry">
                <apex:inputField value="{!acc.Industry}"/>
            </apex:column>
        </apex:pageBlockTable>
        
        <apex:pageBlockButtons >
            <apex:commandButton value="Add New Row" action="{!addAccount}"/>
            <apex:commandButton value="Account Save" action="{!saveAccount}"/>
        </apex:pageBlockButtons>
        
    </apex:pageBlock>
</apex:form>
</apex:page>
ApexClass: 
public class AddmultipleAccountsController {
    Account account = new Account();
    public list<Account> listAccount{ get; set;}
    public List<Account> insrtAccLst = new List<Account>();
    public boolean msgForUser{get;set;}

    public AddmultipleAccountsController(){
        listAccount=new list<Account>();
        listAccount.add(account);
    }

    Public void addAccount(){
        Account acc = new Account();
        listAccount.add(acc);
    }
    
    public PageReference saveAccount() {
        for(Integer i=0; i<=listAccount.size(); i++){
            insrtAccLst.add(listAccount[i]);
        }
        msgForUser = true;
        insert insrtAccLst;
        return null;

}
}
Hope this helps.

Thanks,
Nagendra

 
Abdul KhatriAbdul Khatri
Not sure what is the background but here is a simple class as per you request
 
public class AccountService {

    public void createAccountWithContact () {
        
        Account account = new Account (Name = 'Test Account');
        insert account;
        List<Contact> contactList = new List<Contact>();
        for(Integer i = 0 ; i < 10 ; i++){
            
            Contact contact = new Contact (LastName = 'Test' + String.valueOf(i), AccountId = account.Id,
                                            Email = 'testEmail' + String.valueOf(i) + '@test.com');
            contactList.add(contact);
        }
        insert contactList;
    }
}

 
Ajay K DubediAjay K Dubedi
Hi Veerchand,
Please try this simple code and it will surely work:

Account a=new Account();
a.name='New created account';
insert a;
Integer i;
List<Contact> conList=new List<Contact>();
for(i=1;i<=10;i++)
{
    Contact c=new Contact();
    c.lastname='Contact'+i;
    c.accountid=a.id;
    conList.add(c);
}
insert conList;

I hope this solution will help you. Mark it as best answer if it helps.
Thanks.
Ajay Dubedi