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

VF Page to create records default to 7 rows

I have a simple class and vf page which creates new child custom object records. Currently you press add/remove rows to add or remove another row for a new record.


The parent object is lead__c, the child object is lead_debt__c. The vf page is called by a button on the lead record called add debt.


I am trying to default it to 7 rows and then the user can remove any rows they do not use. I cannot seem to get this to work, can someone please lead me in the right direction?




public class addDebt {

    public List<Lead_Debt__c> debts {get; set;}
    private final Lead__c lead;
    public addDebt(ApexPages.StandardController myController) {
        debts = new List<Lead_Debt__c>();
        Lead_Debt__c debt = new Lead_Debt__c();
        debt.Lead__c =;
    public Lead__c getLead()
    	return [SELECT First_Name__c, Name, Last_Name__c FROM Lead__c WHERE Id = :lead.Id];

    public void addrow() {
        Lead_Debt__c debt = new Lead_Debt__c();
        debt.Lead__c =;
    public void removerow(){
        Integer i = debts.size();
    public PageReference save() {
        	upsert debts;
    	catch (Exception ex)
    		ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Error saving debt. Please notify the systems administrator.' + ex));
        //PageReference home = new PageReference('/home/home.jsp');
        //return home; }}
        PageReference parrec = new PageReference('/';
        return parrec; 
    public static testmethod void TestAddDebt()
    	PageReference pageRef = Page.addDebt;
    	Lead__c lead = Unit_Tests.CreateTestLead();
        // Add parameters to page URL      
        ApexPages.currentPage().getParameters().put('id', lead.Id); 
        // test all functionality     
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(lead);   
        addDebt dbtPage = new addDebt(sc);

 VF Page:


<apex:page standardController="Lead__c" extensions="addDebt">
    <apex:form >
    <apex:pageBlock title="Add Debt" >
    <apex:messages style="color:red;font-weight: bolder;" /> 
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="error" />
                <apex:commandButton value="Cancel" action="{!cancel}" />
            <apex:pageBlockTable value="{!debts}" var="a" id="table">
                  <apex:column headerValue="Creditor">
                    <apex:inputField value="{!a.Creditor__c}"/>
                   <apex:column headerValue="Balance">
                    <apex:inputField value="{!a.Current_Balance__c}"/>
                <apex:column headerValue="Account Number">
                    <apex:inputField value="{!a.Account_Number__c}"/>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" />
            <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                




First let me ask you this, were you able to make it work  for a single row?

In yes then probably running that code over 7 iterations should solve your problem.




Yes, this works with one row. Probably a stupid question but how would I do that?