1

My SF page. So the problem is that I got repeat block, that adds rows by a button on the page. So there is no record inside the list but I got 1 repeat block. If I press "add new row" once there would be only 1 record. So when I save changes I always get one less record than I should. I was trying to add something like this :

Estimates__c estimates = (Estimates__c)con.getRecord();

Estimate_lines__c line.Estimate__c = estimates.Id();

to set id of the parent object to add additional record manually. But it doesn't work. Also it doesn't work if I add it in the constructor. So question is how could I make number of block the same as records. So I always have 1 or more repeat blocks on the page.

VF Page

    <apex:pageBlock id="pageBlock" title="Page created by Me" mode="edit">
        <apex:pageBlockButtons id="Section1_buttons_block">
            <apex:commandButton id="Save_data" value="Save" action="{!save}"/>
            <apex:commandButton id="clear" value="clear" onclick="clearValue()"/>
            <apex:commandButton id="clearAllValuesRerender" value="clearFields" action="{!clearAllFields}" immediate="true"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection id="pageBlockSection1" showHeader="true" title="Section1">
            <apex:inputField id="Project_input_field" value="{!Estimates__c.Project__c}"/>
            <apex:inputField id="Customer_input_field" value="{!Estimates__c.Customer__c}"/>
            <apex:inputField id="Contact_input_field" value="{!Estimates__c.Contacts_Person__c}"/>
            <apex:inputField id="Estimate_status_input_field" value="{!Estimates__c.Status__c}"/>
            <apex:inputField id="Note_input_field" value="{!Estimates__c.Notes__c}"/>
            <apex:commandButton id="New_row_add_button" value="Add new row" action="{!addSection}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection id="pageBlockSection2" showHeader="true" title="Section2">
            <apex:inputField id="Quantity_from_estimates_line" value="{!item.Quantity__c}" />
            <apex:inputField id="Unit_price_input_field" value="{!item.Unit_price__c}" />
            <apex:inputField id="Description_input_field" value="{!item.Description__c}"/>
        </apex:pageBlockSection>
            <br>
                <apex:repeat value="{!itemList}" var="v"> 
                    <apex:pageBlockSection >
                    <apex:inputField id="Quantity_from_estimates_line" value="{!v.Quantity__c}" />
                    <apex:inputField id="Unit_price_input_field" value="{!v.Unit_price__c}" />
                    <apex:inputField id="Description_input_field" value="{!v.Description__c}"/>
                    </apex:pageBlockSection>
                </apex:repeat>
            </br>


    </apex:pageBlock>

My controller.

public with sharing class EstimateController {
    Integer estimateLineItemsCount = 1;
    public Estimates__c estimates {get;set;}
    public List<String>listOfSections {get;set;}
    public List<Estimate_lines__c> itemList {get;set;}
    public Estimate_lines__c item {get;set;}
    public List<Estimate_lines__c> line1 {get;set;} 
    private ApexPages.StandardController con;

    public EstimateController(ApexPages.StandardController con) {
        this.con = con;
        estimates = new Estimates__c();
        itemlist = new List<Estimate_lines__c>();
        item = new Estimate_lines__c();
    }

    public void getEstimateId(){
        Estimates__c estimates = (Estimates__c)con.getRecord();
        item.Estimates__c = estimates.Id;
    } 
    public PageReference addSection(){
        //item = new Estimate_lines__c();
        addLineItem(item);
        return null;
    }
    public PageReference addLineItem(Estimate_lines__c myLine){
        itemList.add(myLine);
        return null;
    }
    public PageReference save(){
        upsert estimates;
        for(Estimate_lines__c line : itemList){
            Estimates__c estimates = (Estimates__c)con.getRecord();
            line.Estimates__c = estimates.Id;
        addLineItem(line);
        }
        Estimates__c estimates = (Estimates__c)con.getRecord();
        item.Estimates__c = estimates.Id;
        addLineItem(item);
        upsert itemList;
        System.debug(itemList);

        return null;
    }
    public PageReference clearAllFields(){
        Estimates__c estimates = (Estimates__c)con.getRecord();
        estimates.Customer__c = null;
        estimates.Project__c = null;
        estimates.Contacts_Person__c = null;
        estimates.Status__c = null;
        estimates.Notes__c = null;

        item.Quantity__c = null;
        item.Unit_price__c = null;
        item.Description__c = null;

        estimateLineItemsCount = 1;

        return null;

    }
}

enter image description here

  • Sorry for bad editing i am new to it. – Void Aug 20 '15 at 17:20
0

Your controller has a lot going on in it that doesn't need to be there. I have made an attempt to clean up the controller and the VF page below as well as commenting it to describe what is happening.

To answer the specific problem you asked about, there will be one <apex:pageBlockSection> added by the repeater for each one of the Estimate Line records in the list that it is iterating over.

Page Controller

public with sharing class EstimateController {

    public Estimates__c estimate { get; set; }
    public List<Estimate_lines__c> estimateLines { get; set; }
    private ApexPages.StandardController con;

    public EstimateController(ApexPages.StandardController con) {
        this.con = con;

        // init the record for the page and the list of children    
        estimate = (Estimates__c)con.getRecord();
        estimateLines = new List<Estimate_lines__c>();

        // add an empty line to the collection initially
        addNewLineItem();
    }

    // add a new item to the list of estimate lines
    public void addNewLineItem() {
        estimateLines.add(new Estimate_lines__c());
    }

    public PageReference save() {

        // DML for the parent record
        upsert estimate;

        // DML for the children

        for (Estimate_lines__c estimateLine : estimateLines) {
            // set the id of the parent on the child
            estimateLine.Estimates__c = estimate.Id;
        }

        upsert estimateLines;

        return null;
    }

    public PageReference clearAllFields(){

        // reset the parent
        // estimate = (Estimates__c)con.getRecord();
        estimate.Customer__c = null;
        estimate.Project__c = null;
        estimate.Contacts_Person__c = null;
        estimate.Status__c = null;
        estimate.Notes__c = null;

        // clear the list of children and then add a new entry to the list
        /* this code needs to be extended to check for an Id value on 
           the Estimate_lines__c records and delete those with Ids from the
           database rather than just clearing the list used by the page */
        estimateLines = new List<Estimate_lines__c>();
        addNewLineItem();

        return null;

    }
}

VF Page

<apex:pageBlock id="pageBlock" title="Page created by Me" mode="edit">
    <apex:pageBlockButtons id="Section1_buttons_block">
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Clear" action="{!clearAllFields}" immediate="true" />
    </apex:pageBlockButtons>

    <!-- create one pageblocksection for the header -->
    <apex:pageBlockSection id="pageBlockSection1" showHeader="true" title="Estimate Header">
        <apex:inputField id="Project_input_field" value="{!estimate.Project__c}"/>
        <apex:inputField id="Customer_input_field" value="{!estimate.Customer__c}"/>
        <apex:inputField id="Contact_input_field" value="{!estimate.Contacts_Person__c}"/>
        <apex:inputField id="Estimate_status_input_field" value="{!estimate.Status__c}"/>
        <apex:inputField id="Note_input_field" value="{!estimate.Notes__c}"/>
        <apex:commandButton id="New_row_add_button" value="Add new row" action="{!addNewLineItem}"/>
    </apex:pageBlockSection>

    <!-- create one pageblocksection for each estimateLine -->
    <apex:repeat value="{!estimateLines}" var="line"> 
        <apex:pageBlockSection id="pageBlockSection2" showHeader="true" title="Estimate Line">
            <apex:inputField id="Quantity_from_estimates_line" value="{!line.Quantity__c}" />
            <apex:inputField id="Unit_price_input_field" value="{!line.Unit_price__c}" />
            <apex:inputField id="Description_input_field" value="{!line.Description__c}"/>
        </apex:pageBlockSection>
    </apex:repeat>

</apex:pageBlock>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.