1

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()

enter image description here

Below is the 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());
            }   
        } 
    }
// This method is called
    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();
        }
    }

Below is the vf page -

<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:outputPanel>
                </apex:outputPanel>
        </div>
    </div>
    </apex:form>
</apex:page>
6
  • Hi all.. can anyone tell where the issue is happening... Aug 25, 2017 at 2:23
  • 1
    Please do not add useless comments or make spurious edits to draw attention to your post. If you would like to increase visibility, the appropriate course of action is to offer a bounty.
    – Adrian Larson
    Aug 25, 2017 at 4:25
  • sure..Adrian...I was stuggling hoping someone can help me in this issue.. Aug 25, 2017 at 4:27
  • Yeah that much is clear. You have been on this site long enough you should understand the guidelines though and operate within them.
    – Adrian Larson
    Aug 25, 2017 at 4:27
  • Why such complex wrapper structure? And have you debugged your code to see if contactWrapperlist has right values after addContactEmail method? Aug 25, 2017 at 5:07

1 Answer 1

3

Define a constructor for ContactEmail class and create a new instance of Contact in it. That's it!

public class ContactEmail{
        public Contact c1 {get;set;}        
        public ContactEmail()
        {
            c1 = new Contact();
        }
    }

Results:

It will retain the values

Contact wrapper

3
  • So Santanu... Is it not possible using Contact object itself ?.. Because tomorrow if i need to add 100 fields then i need to create 100 variables...Is it possible to change the structure of the wrapper class... Aug 25, 2017 at 6:02
  • Hi Santanu.. u saved my day... Can u let me know wht was the issue.. Aug 25, 2017 at 6:12
  • since, contact instance was not getting created, thats why erasing the values Aug 25, 2017 at 6:13

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.