0

I want to insert multiple records at a time, but whenever i try to insert a record and add to table all previous records get changed to the recent one.enter image description here

Here's the code enter image description here

VF Page

<apex:page standardController="Position__c" extensions="PositionExtension">
    <apex:form >
        <apex:pagemessages ></apex:pagemessages>
        <apex:pageBlock >            
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!Add_Row}" value="Add Row" rerender="positions" />
            </apex:pageBlockButtons>

            <apex:pageBlockSection title="Position" columns="2">
                <apex:inputField value="{!Position__c.Name}" required="true"/>
                <apex:inputField value="{!Position__c.Status__c}"/>
                <apex:inputField value="{!Position__c.Location__c}"/>
                <apex:inputField value="{!Position__c.Type__c}"/>
                <apex:inputField value="{!Position__c.Open_Date__c}"/>
                <apex:inputField value="{!Position__c.Hire_By__c}"/>
                <apex:inputField value="{!Position__c.Close_Date__c}"/>
                 <apex:pageBlockSectionItem /> <apex:pageBlockSectionItem /> 
                <apex:inputField value="{!Position__c.Functional_Area__c}"/>
                <apex:pageBlockSectionItem /> 
                <apex:inputField value="{!Position__c.Job_Level__c}"/>
                <apex:pageBlockSectionItem /> <apex:pageBlockSectionItem /> 
                <apex:inputField value="{!Position__c.Min_Pay__c}"/>
                <apex:inputField value="{!Position__c.Max_Pay__c}"/>
            </apex:pageBlockSection>   
        </apex:pageBlock>

        <apex:pageBlock >
           <apex:pageBlockTable id="positions" value="{!allrecords}" var="Position">
                <apex:column value="{!Position.Name}"/>
                <apex:column value="{!Position.Status__c}"/>
                <apex:column value="{!Position.Location__c}"/>
                <apex:column value="{!Position.Type__c}"/>
                <apex:column value="{!Position.Open_Date__c}"/>
                <apex:column value="{!Position.Hire_By__c}"/>
                <apex:column value="{!Position.Close_Date__c}"/>
                <apex:column value="{!Position.Job_Level__c}"/>
                <apex:column value="{!Position.Functional_Area__c}"/>
                <apex:column value="{!Position.Min_Pay__c}"/>
                <apex:column value="{!Position.Max_Pay__c}"/>
            </apex:pageBlockTable>

            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>


        </apex:pageBlock>

    </apex:form>
</apex:page>
1
  • Can you post the VF code as well? Also are you re-rendering the list of records? May 18 '17 at 12:13
1

The reason behind this is record is not getting instantiate again. So you need to instantiate record again with new. Update Add_Row method as below.

Position__c anotherRecord = new Position__c();
anotherRecord.Name = record.Name;
anotherRecord.Status__c = record.Status__c;
/*
  similarly copy other fileds as well for this new insatance
*/
allrecords.add(anotherRecord)
5
  • not working! after implementing this, it is redefining the allrecords list too. May 18 '17 at 12:45
  • UPDATE! As the record is set in constructor, it get executed only once, and when we reinitialize record , record returns empty for every Add_Row() method call. May 18 '17 at 12:53
  • @SaumyaGaikwad pls check above answer I have updated it. Because every time while adding records to list it suppose to be new. I hope this will help you. May 18 '17 at 13:19
  • Great! wow.... I had almost lost hope on this! Thankyou May 19 '17 at 4:27
  • Is there any way to iterate over the fields available in the current context of controller to this scenario. I mean all the fields accessed in the VF will be available in the 'record = (Position__c)controller.getRecord(); ' context. Any way to iterate the fields too? May 19 '17 at 4:57

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.