Skip to main content
Apex code:

 

public class AddContact {

 

   ApexPages.StandardController sc;

 

   public Account acct{get;set;}

 

   public Integer marker=2;

 

   public Integer selectedContact{get;set;}

 

   public List<WrapperClass> lContacts{get;set;}

 

   public integer noOfRecordsToInsert {get;set;}

 

    

 

   public AddContact(ApexPages.StandardController controller) {

 

       this.acct = (Account)controller.getRecord();

 

       sc=controller;

 

      // for(integer i=0;i<=noOfRecordsToInsert;i++){

 

       lContacts=new List<WrapperClass>();

 

       Contact c=new Contact();

 

       lContacts.add(new WrapperClass(c,0));

 

  // }

 

 }

 

   public PageReference deleteContact(){

 

       Integer x=-1;

 

       for(WrapperClass wc:lContacts){

 

           x++;

 

           if(wc.counter==selectedContact){

 

               System.debug('-->selected contact:'+selectedContact+'  position:'+x);

 

               break;

 

           }

 

       }

 

       lContacts.remove(x);

 

       return null;

 

   }

 

   public PageReference saveAccount(){

 

   try

 

   {

 

   system.debug('try block');

 

        Database.SaveResult sr = Database.insert(acct, true);

 

     

 

           id idey=sr.getId();

 

       integer i;

 

     

 

           system.debug('account'+sr);

 

       List<Contact> contactList=new List<Contact>();

 

      // for(i=0;i<noOfRecordsToInsert;i++){

 

          // if(noOfRecordsToInsert!=null){

 

       for(WrapperClass wc:lContacts){

 

         i++;

 

       //account a=new account();

 

           Contact c=new Contact();

 

       c.lastName=wc.c.lastName;

 

       c.firstName=wc.c.firstName;

 

       c.Phone=wc.c.Phone;

 

       c.Email=wc.c.Email;

 

       c.accountid=idey;

 

       contactList.add(c);

 

           contactList.get(noOfRecordsToInsert);

 

      // }

 

           //}

 

       insert contactList;

 

       }

 

   }

 

       catch(exception e)

 

       {

 

           system.debug('catch block');

 

     apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getmessage()));

 

       }

 

       

 

       return null;

 

   }

 

   public PageReference addAContact(){

 

              for(integer i=0;i<noOfRecordsToInsert;i++){

 

       Contact c=new Contact();

 

      

 

       lContacts.add(new WrapperClass(c,marker));

 

       //lContacts.add(new WrapperClass(c,noOfRecordsToInsert));

 

           

 

       marker=marker+1;

 

  

 

              }

 

     

 

       return null;

 

   }

 

   public class WrapperClass{

 

       public Integer counter{get;set;}

 

       public Contact c{get;set;}

 

       public WrapperClass(Contact cntc,Integer i){

 

           this.c=cntc;

 

           this.counter=i;

 

       }

 

   }

 

}

 

my vf :

 

<apex:page standardController="Account" extensions="AddContact">

 

    <apex:form id="myForm">

 

    <apex:panelGrid columns="2">

 

    Account Name: <apex:inputField value="{!account.name}" />

 

        no of contacts: <apex:inputText value="{!noOfRecordsToInsert}" />

 

        <apex:actionSupport event="onchange" action="{!addAContact}"       rerender="myForm"/> 

 

    </apex:panelGrid>

 

         

 

    <apex:repeat value="{!lContacts}" var="x">

 

    <apex:panelGrid columns="8">

 

      

 

    First Name:<apex:inputField value="{!x.c.firstName}" />

 

    Last Name:<apex:inputField value="{!x.c.lastName}" />    

 

    Lead Source:<apex:inputField value="{!x.c.leadSource}" /> 

 

    

 

    <apex:commandButton action="{!deleteContact}" style="Button" value="Delete Contact" reRender="myForm" immediate="true">

 

<apex:param value="{!x.counter}" name="selected"

 

assignTo="{!selectedContact}"/>

 

</apex:commandButton>      

 

    </apex:panelGrid>

 

    </apex:repeat>

 

    <apex:commandButton value="Add Contact" action="{!addAContact}" reRender="myForm" immediate="true"/> 

 

       

 

    <apex:commandButton value="Save" action="{!saveAccount}"  />    

 

    </apex:form> 

 

</apex:page>

 

Please help me how to do for loop to create insert row based on input no of records to insert
2 个回答