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;
}
}