0

I have created an input form. The input form fields are Name, Status, Start time and End time which are the fields of Deliverable object. On click of Add button the values entered in the input text fields have to be displayed in the table. Here is the code i have written:

Controller:

public with sharing class TBN_ProjectDeliverable {

public List<Project__c> lstProjects = new List<Project__c>();
public List<Delivarable__c> lstDeliverables {get; set;}
public Delivarable__c objDelivarable = new Delivarable__c();
public String selectedProject {get; set;}
public String inputName {get; set;}
public String inputStatus {get; set;}
public String inputStartTime {get; set;}
public String inputEndTime {get; set;}
public boolean showInputForm {get; set;}
public boolean showTable {get; set;}
public Integer counter = 0;

public TBN_ProjectDeliverable(){

    lstProjects =   ([  SELECT Name 
                        FROM Project__c
                        WHERE Status__c = 'Active'
                    ]);
    lstDeliverables = new List<Delivarable__c>();
}

public List<SelectOption> getActiveProjects(){

    List<SelectOption> lstSelectOptions = new List<SelectOption>();

    for(Project__c objProject: lstProjects) {

        lstSelectOptions.add(new SelectOption(objProject.Name , objProject.Name));
    }

    return lstSelectOptions;
}

public void btnGo() {

    showInputForm = true;
}

public Delivarable__c getDelivarable {

    get{

        System.debug('=====objDelivarable======'+objDelivarable);
        return objDelivarable;

    } set;
}

public pageReference addDataToTable() {

    showTable = true;
    System.debug('=====objDelivarable======'+objDelivarable);
    lstDeliverables.add(objDelivarable);
    System.debug('=====lstDeliverables======'+lstDeliverables);
    System.debug('=====lstDeliverables.Name======'+lstDeliverables[0].Name);

    return null;
}

public void btnEdit(){

}

public void btnSave() {

    Database.insert(objDelivarable);
}

public void btnCancel() {

}
}

Vf page:

<apex:page Controller="TBN_ProjectDeliverable" sidebar="false" tabStyle="Project__c">
<apex:form >
    <apex:pageBlock title="Project Deliverable" id="thePageBlock">
        <apex:pageBlockSection >
            <apex:selectList label="Active Projects:" value="{!selectedProject}" size="1"> 
                <apex:selectOptions value="{!ActiveProjects}"/>
            </apex:selectList>
            <apex:commandButton action="{!btnGo}" value="Go"/>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Create deliverable record" rendered="{!showInputForm}">
            <apex:inputfield value="{!getDelivarable.Name}"/>
            <apex:inputfield value="{!getDelivarable.Status__c}"/>
            <apex:inputfield value="{!getDelivarable.Start_Time__c}"/>
            <apex:inputfield value="{!getDelivarable.End_time__c}"/>
            <apex:commandButton action="{!addDataToTable}" value="Add" rerender="thePageBlockTable, thePageBlock"/>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Table of deliverable records" rendered="{!showTable}" columns="1" id="thePageBlockTable">
            <apex:pageBlockTable value="{!lstDeliverables}" var="objLstDeliverables" >
                <apex:column headervalue="Name">
                    <apex:outputText value="{!objLstDeliverables.Name}"/>
                </apex:column>
                <apex:column headervalue="Status">
                    <apex:outputText value="{!objLstDeliverables.Status__c}"/>
                </apex:column>
                <apex:column headervalue="Start Time">
                    <apex:outputText value="{!objLstDeliverables.Start_Time__c}"/>
                </apex:column>
                <apex:column headervalue="End Time">
                    <apex:outputText value="{!objLstDeliverables.End_time__c}"/>
                </apex:column>
                <apex:column headervalue="Edit">
                    <apex:commandLink value="Edit" action="{!}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:commandButton action="{!btnSave}" value="Save" />
            <apex:commandButton action="{!btnCancel}" value="Cancel"/>  
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>        
</apex:page>

I am getting the input form data in the table. But on re-entering the form data with new values, the previous data is getting overriden in the table. Please help to to solve this problem.

2

I think your problem is that you only have one objDelivarable.

You get it into the form with getDelivarable, set its values, and then add it to the list in addDataToTable. Because objDelivarable is always the same instance of Delivarable__c, there's only one instance to add to the list.

This would probably work better if after you've added objDelivarable into the list, you instantiate a new Delivarable__c for the VF page to work on:

public pageReference addDataToTable() {

    showTable = true;
    System.debug('=====objDelivarable======'+objDelivarable);
    lstDeliverables.add(objDelivarable);
    System.debug('=====lstDeliverables======'+lstDeliverables);
    System.debug('=====lstDeliverables.Name======'+lstDeliverables[0].Name);

    // create a new instance
    objDelivarable = new Delivarable__c();

    return null;
}

You should also note that in your save method you're only inserting one instance. I think you probably want to insert the whole list:

public void btnSave() {

    Database.insert(lstDelivarable);
}
|improve this answer|||||
  • Yes. On click of save i have to insert all the records present in the table. – Srilakshmi B S Sep 11 '14 at 7:44
  • public void btnEdit(){ System.debug('=====rowNumber===='+rowNumber); objDelivarable = lstDeliverables.get(rowNumber); System.debug('=====lstDeliverables.get(rowNumber)===='+objDelivarable); lstDeliverables.remove(rowNumber); } I wrote this function for edit button. When i click on edit, the particular row data has to be put back to the input form. In this code i am getting the data in objDelivarable. But i am not getting how to place it back to the form. Please help me. – Srilakshmi B S Sep 11 '14 at 11:04
  • I got the solution for edit button. public void btnEdit(){ System.debug('=====rowNumber===='+rowNumber); objDelivarable = lstDeliverables.get(rowNumber); System.debug('=====lstDeliverables.get(rowNumber)===='+objDelivarable); lstDeliverables.remove(rowNumber); getDelivarable = objDelivarable; System.debug('=====getDelivarable.Name===='+getDelivarable.Name); //return objDelivarable; } rerender the form on click of edit. – Srilakshmi B S Sep 11 '14 at 13:15

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.