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
jburnsSTjburnsST 

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?

 

CLASS:

 

public class addDebt {

    public List<Lead_Debt__c> debts {get; set;}
    private final Lead__c lead;
    public addDebt(ApexPages.StandardController myController) {
        lead=(Lead__c)myController.getrecord();
        debts = new List<Lead_Debt__c>();
        Lead_Debt__c debt = new Lead_Debt__c();
        debt.Lead__c = lead.id;
        debts.add(debt);}
        
    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 = lead.id;
        debts.add(debt);}
            
    public void removerow(){
        Integer i = debts.size();
        debts.remove(i-1);}
            
    public PageReference save() {
    	try
    	{
        	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');
        //home.setRedirect(true);
        //return home; }}
        PageReference parrec = new PageReference('/'+lead.id);
        parrec.setRedirect(true);
        return parrec; 
    }
    
    public static testmethod void TestAddDebt()
    {
    	PageReference pageRef = Page.addDebt;
	    Test.setCurrentPage(pageRef);
    
    	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);
        dbtPage.addrow();
        dbtPage.removerow();
        dbtPage.save(); 
    }
 }

 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:pageBlockButtons>
            <apex:pageBlockTable value="{!debts}" var="a" id="table">
                  <apex:column headerValue="Creditor">
                    <apex:inputField value="{!a.Creditor__c}"/>
                </apex:column>
                   <apex:column headerValue="Balance">
                    <apex:inputField value="{!a.Current_Balance__c}"/>
                </apex:column>
                <apex:column headerValue="Account Number">
                    <apex:inputField value="{!a.Account_Number__c}"/>
                </apex:column>
                
               </apex:pageBlockTable>
               
               
               
    <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" />
&nbsp;|&nbsp;&nbsp;
            <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

IspitaIspita

Hi,

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.

 

 

jburnsSTjburnsST

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