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
sfdclearner123sfdclearner123 

Add dynamic Rows in vf page is clearing previous rows values

I am having this below code in which i am trying to add dynamic rows. But when i click add Contact Email button it is clearing the previous rows.
Below Method is called from add Contact Email button.

addContactEmail()

Apex Class - 
 
public class accountcontactwrapperclass {

    public Account acc {get;set;}
    public List<ContactWrapper> contactWrapperlist {get;set;}
    public List<Contact> cList;
    public String wrapperId {get; set;}

    public accountcontactwrapperclass (ApexPages.StandardController controller){
        contactWrapperlist = new List<ContactWrapper>();
        acc = new Account();
        cList = new List<Contact>();
        addContact();
    }

    public void addContact(){

        ContactWrapper contWrap = new ContactWrapper();
        ContactEmail contactEmailwrap = new ContactEmail();
        contWrap.contactEmails.add(contactEmailwrap);
        contactWrapperlist.add(contWrap);

    }

    public void addContactEmail(){
        for(ContactWrapper s : contactWrapperlist){
            if(s.wrapperId == wrapperId){
                 s.contactEmails.add(new ContactEmail());
            }   
        } 
    }

    public class ContactEmail{        
        public Contact c1 {get;set;}
    }

    public class ContactWrapper{
        public Contact c {get;set;}
        public string wrapperId {get;set;}
        public List<ContactEmail> contactEmails {get;set;}
        public ContactWrapper(){
            Blob b = Crypto.GenerateAESKey(128);
            String h = EncodingUtil.ConvertTohex(b);
            wrapperId = h.SubString(0,8)+ '-' + h.SubString(8,12) + '-' + h.SubString(12,16) + '-' + h.SubString(16,20) + '-' + h.substring(20);
            contactEmails = new List<ContactEmail>();
            c = new Contact();
        }
    }

}


User-added image
Samudhraa GSamudhraa G
Hi,
I don't know how your vf page looks like, so I am not sure from where you are calling this method.
But usually the previous values are cleared out, when the list is initialized, during your call. So in your case, the accountwrapper() constructor is called each time, and the list is initialized, and then the addContactEmail is called.
Try adding a debug line at the constructor to see if it gets called each time the button is clicked, or try a debug line with contactWrapperlist.size() to confirm this.
You can change your code accordingly then.

Hope that helps
sfdclearner123sfdclearner123
Below is the vf page i am using - 
 
<apex:page standardController="Account" extensions="accountcontactwrapperclass">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script>
    <apex:form >
               
    <div class="panel panel-default">
            <div class="panel-heading backgroundTheme" style="font-size: 16px; font-weight: bold">Account</div>
            <div class="panel-body">
                <apex:outputPanel id="accountname">
                    <div class="row">
                        <div class="col-md-3">
                            <h5>
                                <div class="field_label " id="closedate">Account Name</div>
                            </h5>
                            <div>
                                <apex:inputField value="{!acc.Name}" label="CloseDate" styleClass="form-control"/>
                            </div>
                        </div>
                    </div>
                    <apex:outputPanel id="panelId">
                        <div class="panel panel-default" id="subs">
                            <div class="panel-heading backgroundTheme" style="font-size: 16px; font-weight: bold">Contact</div>
                            <div class="panel-body" >
                                <apex:repeat value="{!contactWrapperlist}" var="wrappers">
                                    <div class="row">
                                        <div class="col-md-3">
                                            <apex:outputLabel style="margin-bottom: 0px;font-weight: inherit;" >Last Name</apex:outputLabel>
                                            <div class="form-group">
                                                <apex:inputText value="{!wrappers.c.LastName}"/>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="panel panel-default" id="subs">
                                        <div class="panel-heading backgroundTheme" style="font-size: 16px; font-weight: bold">Contact Email</div>
                                        <div class="panel-body" >
                                            <apex:repeat value="{!wrappers.contactEmails}" var="wrappers1">
                                            <div class="row">
                                                <div class="col-md-3">
                                                    <apex:outputLabel style="margin-bottom: 0px;font-weight: inherit;" >Email</apex:outputLabel>
                                                    <div class="form-group">
                                                        <apex:inputText value="{!wrappers1.c1.Email}"/>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-3">
                                                    <apex:outputLabel style="margin-bottom: 0px;font-weight: inherit;" >Phone</apex:outputLabel>
                                                    <div class="form-group">
                                                        <apex:inputText value="{!wrappers1.c1.Phone}"/>
                                                    </div>
                                                </div>
                                                
                                            </div>
                                            </apex:repeat>
                                        </div>
                                        <div class="col-md-1" style="margin-top: 1.5em;">
                                            <apex:commandbutton value="Add Contact Email" action="{!addContactEmail}" reRender="panelId">
                                                <apex:param name="wrapperId" value="{!wrappers.wrapperId}" assignTo="{!wrapperId}" />
                                            </apex:commandbutton>
                                        </div> 
                                    </div>
                                </apex:repeat>
                            </div>
                        </div>
                        <apex:commandbutton value="Add Contact" action="{!addContact}" reRender="panelId"></apex:commandbutton>
                        <apex:commandButton value="Save" action="{!SaveAll}"></apex:commandButton>
                    </apex:outputPanel>
                </apex:outputPanel>
        </div>
    </div>
    </apex:form>
</apex:page>

Can you check and let me know. 
sfdclearner123sfdclearner123
Can anyone tell where issue is happening...