+1 vote
35 views
by (163k points)

I have a custom controller that allows users to input data in inputFields and add the record to an apex:pageBlockTable by hitting a button to add to the list. I would like to reset these fields each time a user inputs a record so they may enter another record.

Records users input are controlled via a custom controller method that re-instantiates a new record each time, however the previous values still remain in their fields.

enter image description here

How can I reset the fields here each time a user adds a record to the list?

Apex controller

    public with sharing class ecrjcClass{
    
    public Participant__c record {get;set;}
    public List<Participant__c> allrecords {get;set;}
    public Referral__c ref {get;set;}


    public ecrjcClass(){
        record = new Participant__c();
        allrecords = new List<Participant__c>();
        ref = new Referral__c();
    }   
    

    //Add a record row
    public pageReference Add_Row(){

        Participant__c anotherRecord = new Participant__c();   
        anotherRecord.Name = record.Name;
        anotherRecord.Last_Name__c = record.Last_Name__c;
        anotherRecord.Age__c = record.Age__c;
        anotherRecord.Victim_Offender__c = record.Victim_Offender__c;
        anotherRecord.Phone__c = record.Phone__c;
        
        insert ref;
        
        anotherRecord.Referral__c = ref.Id;
        //Issue: Need to insert ref first
        allrecords.add(anotherRecord);
        return null;        
        
    }
    
    //Save and insert records in list
    public pageReference save(){
        
        insert allrecords;
        record = null;
        //This seems to work for the first record added, adding any additional records returns an error
        return null;
            
    }
}

VF markup

    <apex:pageBlock >
    <!--Participant info-->
    <apex:pageBlockSection title="Partipicant Information">
        <apex:inputField value="{!record.Name}"/>
        <apex:inputField value="{!record.Last_Name__c}"/>
        <apex:inputField value="{!record.Age__c}"/>
        <apex:inputField value="{!record.Phone__c}"/>
        <apex:inputField value="{!record.Victim_Offender__c}"/>
    </apex:pageBlockSection>     
    <!--Add participant to record list-->    
        <apex:pageBlockButtons location="bottom">
        <apex:commandButton action="{!Add_Row}" value="Add Participant"/>
    </apex:pageBlockButtons>
    </apex:pageBlock>   
    
            <apex:pageBlock title="Added Participants">
        <apex:pageBlockTable id="Participants" value="{!allrecords}" var="Participant">
            <apex:column headerValue="First Name" value="{!Participant.Name}"/>
            <apex:column headerValue="Last Name" value="{!Participant.Last_Name__c}"/>
            <apex:column headerValue="Age" value="{!Participant.Age__c}"/>
            <apex:column headerValue="Phone" value="{!Participant.Phone__c}"/>
            <apex:column headerValue="Victim/Offender" value="{!Participant.Victim_Offender__c}"/>
        </apex:pageBlockTable>
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!save}" value="Submit"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

1 Answer

+1 vote
by (163k points)
 
Best answer
  1. You can instantiate the record variable (See second last line in the method.)

     //Add a record row
     public pageReference Add_Row(){
    
     Participant__c anotherRecord = new Participant__c();   
     anotherRecord.Name = record.Name;
     anotherRecord.Last_Name__c = record.Last_Name__c;
     anotherRecord.Age__c = record.Age__c;
     anotherRecord.Victim_Offender__c = record.Victim_Offender__c;
     anotherRecord.Phone__c = record.Phone__c;
    
     insert ref;
    
     anotherRecord.Referral__c = ref.Id;
     //Issue: Need to insert ref first
     allrecords.add(anotherRecord);
    
     // ADD THIS LINE
     record = new Participant__c();
     return null;        
    

    }

  2. Then add an id to the first apex:pageBlockSection

<apex:pageBlockSection id="participant" title="Partipicant Information">
  1. And then in the commandbutton rerender the participant Information section
<apex:commandButton action="{!Add_Row}" rerender="participant" value="Add Participant"/>
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...