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
Joe HayesJoe Hayes 

Pass inputfield into controller to pre-populate new row

I am writing a vf page and controller to add multiple child records to the parent at the same time. Sort of like a dynamic related list.
I have a table with 1 row to begin with and a method to add rows as needed.

When the Add row button is pressed I need to pass the values from row 1 into the new row.

I have tried to pass them using 
public string Assessor {get;set;}
but this gets the value when the page is loaded therefore it is empty when I recall it with insertAsstRow()

I need to get the values when the add row button is pressed and then I can populate the new fields from there.

Here is my vf page:
<apex:page controller="AddingAssessmentsController">
    <apex:form >
        <apex:variable var="rowNum" value="{!0}"/>
        <apex:pageBlock >            
            <apex:sectionHeader subtitle="Add Assessments"/>
            <apex:variable var="rowNum" value="{!0}"/>
            <apex:pageBlockTable value="{!AssessmentList}" var="asst">
                <apex:facet name="footer">
                    <apex:commandLink value="Add" action="{!insertAsstRow}"/>
                </apex:facet>
                <apex:column headerValue="Name">
                    <apex:inputField value="{!asst.Name}"/>
                </apex:column>
                <apex:column headerValue="Last Centre Date">
                    <apex:inputField value="{!asst.LastCentreDate__c}" id="LastCentreDate"/>
                </apex:column>  
                <apex:column headerValue="Date of Tech Review">
                    <apex:inputField value="{!asst.DateofTechReview__c}" id="DateOfTechReview"/>
                </apex:column>
                <apex:column headerValue="Date of Received By Cert Office">
                    <apex:inputField value="{!asst.Date_Received_By_Cert_Office__c}" id="Date_Received_By_Cert_Office"/>
                </apex:column>
                <apex:column headerValue="Assessor">
                    <apex:inputField value="{!asst.Assessor__c}" id="Assessor"/>
                </apex:column>
                <apex:column headerValue="Verifier">
                    <apex:inputField value="{!asst.Verifier__c}" id="Verifier"/>
                </apex:column>
                <apex:column headerValue="Photo Scanned">
                    <apex:inputcheckbox value="{!asst.Photo_Scanned__c}" id="photoscanned" selected="true"/>
                </apex:column>
                <apex:column headerValue="Photo Scan date">
                    <apex:inputfield value="{!asst.Photo_Scan_Date__c}" id="photo_scan_date"/>
                </apex:column>
                <apex:column headerValue="Fastrack">
                    <apex:inputcheckbox value="{!asst.Fastrack__c}" id="Fastrack"/>
                </apex:column>
                <apex:column headerValue="MOT Cert">
                    <apex:inputcheckbox value="{!asst.MOT_Cert__c}" id="Mot_cert"/>
                </apex:column>
                <apex:column headerValue="Pass?">
                    <apex:inputcheckbox value="{!asst.Pass__c}" id="pass" selected="true"/>
                </apex:column>
                <apex:column headerValue="Delete" >
                    <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delAsstRow}">
                        <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink>
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
                </apex:column>          
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!insertAssessments}"/>
                <apex:commandButton value="Back" action="{!cancelAssessments}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class AddingAssessmentsController {
    Id candId;
        
    public List<Assessment__c> AssessmentList {get;set;}
    public Integer rowNum{get;set;}
   
    public AddingAssessmentsController(){
        candId = ApexPages.currentPage().getParameters().get('candId');      
        AssessmentList = new List<Assessment__c>();
        AssessmentList.add(new Assessment__c(Candidate__c=candId, pass__c=true, photo_scanned__c=true));
    }
        
    public PageReference insertAssessments(){
        insert AssessmentList;
        PageReference reRender = new PageReference('/'+candId);
        reRender.setRedirect(true);
        return reRender;
    }
    
    public PageReference cancelAssessments(){
        PageReference reRender = new PageReference('/'+candId);
        reRender.setRedirect(true);
        return reRender;
    }
            
    public void insertAsstRow(){
        AssessmentList.add(new Assessment__c(Candidate__c=candId));
    }
    
    public void delAsstRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        AssessmentList.remove(rowNum);   
    }
}

Thanks
Joe
 
NagendraNagendra (Salesforce Developers) 
Hi Joe,

Please find the suggested solution from stack exchange community which works for the above issue.

Get the first row of your list, or whichever row you want to copy, pass it in a temporary variable and then create a new row copying all the values you need from the temporary variable.

Please see here:
public void insertAsstRow(){
        Assesment__c tmpAssesment = AssessmentList.get(rowNum);
        AssessmentList.add(new Assessment__c(Candidate__c=tmpAssesment.candId,
                                     Other_field__c=tmpAssesment.Other_Field__c
                                     ....));
    }
The 'rowNum' would be 0 if you want to copy the first row of your list, or if you want to copy the last record of your list, 'rowNum' would equal to 'AssessmentList.size()-1';

Hope this resolves the issue.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering similar issue.

Thanks,
Nagendra.