0

I have a VFP here that has the ability to add/remove rows of Contacts.  After I click 'Save Contact', I am redirecting the page to a PDF (VFP) that should display the Contacts that were added.  However my table has blank data. Can some one please assist? 

<apex:page sidebar="false" showHeader="false" Controller="wrapperClassAddContacts" renderAs="PDF" >   
        <apex:form >
        <apex:pageBlock title="You added these Contacts">
                <table border="2px">
                        <tr>
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Email</th>
                            <th>Phone</th>
                       </tr>
                  <apex:repeat value="{!contactList}" var="rec" >
                       <tr>
                            <td>{!rec.con.FirstName}</td> 
                            <td>{!rec.con.LastName}</td>
                            <td>{!rec.con.phone}</td>
                       </tr>
                  </apex:repeat>
                </table>
        </apex:pageBlock>

    </apex:form>
</apex:page>

// Add Contacts

<apex:page controller="wrapperClassAddContacts">
            <apex:form >

                <apex:pageBlock id="BlockId" >
                  <apex:pageMessages ></apex:pageMessages>
                  <apex:pageBlockButtons >
                   <apex:commandButton value="Save Contact" action="{!saveContact}" reRender="BlockId"/>
                  </apex:pageBlockButtons>
                 <apex:pageBlockTable value="{!contactList}" var="contact" id="table">
                    <apex:column headerValue="Select">
                        <!-- This is our selected Boolean property in our wrapper class -->
                        <apex:inputCheckbox value="{!contact.selected}"/>
                    </apex:column>
                    <!-- This is how we access the contact values within our cContact container/wrapper -->
                    <apex:column headerValue="First Name">
                        <apex:inputField value="{!contact.con.FirstName}" required="true"/>
                    </apex:column>

                    <apex:column headerValue="Last Name">
                        <apex:inputField value="{!contact.con.LastName}"/>
                    </apex:column>

                    <apex:column headerValue="Email">
                        <apex:inputField value="{!contact.con.Email}" required="true"/>
                    </apex:column>

                <apex:column headerValue="Phone">
                    <apex:inputField value="{!contact.con.Phone}"/>
                </apex:column>

                <apex:column headerValue="Admin1">
                    <apex:inputField value="{!contact.con.Py_Admin__c}"/>
                </apex:column>
                <apex:column headerValue="Admin2">
                    <apex:inputField value="{!contact.con.Sy_Admin__c}"/>
                </apex:column>
                <apex:column headerValue="Primary Tech">
                    <apex:inputField value="{!contact.con.Py_DTP__c}"/>
                </apex:column>
                <apex:column headerValue="Secondary Tech">
                    <apex:inputField value="{!contact.con.Sy_DTP__c}"/>
                </apex:column>
                <apex:column headerValue="Enquiry Contact">
                    <apex:inputField value="{!contact.con.EC__c}"/>
                </apex:column>

                <apex:column headerValue="Billable Contact">
                    <apex:inputField value="{!contact.con.BC__c}"/>
                </apex:column>

                <apex:column headerValue="Add/Remove Contact">
                    <apex:commandButton value="Add" action="{!addContact}" Rerender="table" />
                    <apex:CommandButton value="Remove" action="{!removeContact}" Rerender="table" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

// Controller

public class wrapperClassAddContacts {
    public List<wrapContact> contactList {get; set;}
    public Contact con {get;set;}
    public integer rowIndex {get;set;}


    public wrapperClassAddContacts () {
      rowIndex =0;
      contactList = new List<wrapContact>();
      Contact Cont = new Contact();
      contactList.add(new wrapContact(Cont));

    }
    Public PageReference addContact(){
      rowIndex += 1;
      Contact con = new Contact();
      contactList.add(new wrapContact(con));
      return null; 
    }

    Public void removeContact(){
      contactList.remove(rowIndex);
      rowIndex -= 1;   
    }


public PageReference saveContact() {
    boolean emailexists = false;
    Map<String, Contact> contactEmailMap = new Map<String, Contact>();
    List<Contact> selectedContacts = new List<Contact>();
    list<string> minOnemaxTwoDSA = new list<string> ();

    for(wrapContact cCon: contactList) {
        if(cCon.selected == true){ 
           if(contactEmailMap.containsKey(cCon.con.Email)) {
                cCon.con.Email.addError('Another Contact has same email address');
                emailexists = true;
            } else {
                contactEmailMap.put(cCon.con.Email,cCon.con);
            }
        }    
    }

    selectedContacts.addAll(contactEmailMap.values());

    if(emailexists ==false && selectedContacts.size() > 0  ){
            insert selectedContacts;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Contact Saved succesfully'));
            contactList=null;
    }else{
        if(selectedContacts.isEmpty()){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Select atleast one contact'));
        }
   }        

    PageReference displayContactsPage = Page.DisplayContacts;
    displayContactsPage .setRedirect(true);
    return displayContactsPage ;


}

    public class wrapContact {
      public Contact con {get; set;}
      public Boolean selected {get; set;}
      public Boolean isDSA {get;set;}

        public wrapContact(Contact c) {
          con = c;
          selected = false;

        }
    }
}

// the PDF that is rendered

enter image description here

1
  • so, you have two VFP sharing the same controller? If yes, when the second (PDF) VFP is rendered, it will instantiate a brand new instance of the controller and needs to fetch the contacts -- adding them to the contactList variable
    – cropredy
    Sep 25, 2019 at 3:33

2 Answers 2

2

There are couple of things you have to fix it.

1) If you want to share variables between two VF pages you have to use .setRedirect(false);

If setRedirect = true

A redirect is performed through a client side redirect. This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST

If setRedirect = false

The redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

2) After inserting the contacts you are making the contactList=null;. Remove it.

3) You are using reRender="BlockId" which is not required here as you are re-directing to a new page.

Updated Code

<apex:page controller="wrapperClassAddContacts">
    <apex:form >
        <apex:pageBlock id="BlockId" >
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save Contact" action="{!saveContact}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!contactList}" var="contact" id="table">
                <apex:column headerValue="Select">
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!contact.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column headerValue="First Name">
                    <apex:inputField value="{!contact.con.FirstName}" required="true"/>
                </apex:column>

                <apex:column headerValue="Last Name">
                    <apex:inputField value="{!contact.con.LastName}"/>
                </apex:column>

                <apex:column headerValue="Email">
                    <apex:inputField value="{!contact.con.Email}" required="true"/>
                </apex:column>

                <apex:column headerValue="Phone">
                    <apex:inputField value="{!contact.con.Phone}"/>
                </apex:column>

                <apex:column headerValue="Admin1">
                    <apex:inputField value="{!contact.con.Py_Admin__c}"/>
                </apex:column>
                <apex:column headerValue="Admin2">
                    <apex:inputField value="{!contact.con.Sy_Admin__c}"/>
                </apex:column>
                <apex:column headerValue="Primary Tech">
                    <apex:inputField value="{!contact.con.Py_DTP__c}"/>
                </apex:column>
                <apex:column headerValue="Secondary Tech">
                    <apex:inputField value="{!contact.con.Sy_DTP__c}"/>
                </apex:column>
                <apex:column headerValue="Enquiry Contact">
                    <apex:inputField value="{!contact.con.EC__c}"/>
                </apex:column>

                <apex:column headerValue="Billable Contact">
                    <apex:inputField value="{!contact.con.BC__c}"/>
                </apex:column>

                <apex:column headerValue="Add/Remove Contact">
                    <apex:commandButton value="Add" action="{!addContact}" Rerender="table" />
                    <apex:CommandButton value="Remove" action="{!removeContact}" Rerender="table" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

PDF Page

<apex:page sidebar="false" showHeader="false" Controller="wrapperClassAddContacts" renderAs="PDF" >   
    <apex:form >
        <apex:pageBlock title="You added these Contacts">
            <table border="2px">
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Email</th>
                    <th>Phone</th>
                </tr>
                <apex:repeat value="{!contactList}" var="rec" >
                    <tr>
                        <td>{!rec.con.FirstName}</td> 
                        <td>{!rec.con.LastName}</td>
                        <td>{!rec.con.Email}</td>
                        <td>{!rec.con.phone}</td>
                    </tr>
                </apex:repeat>
            </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public class wrapperClassAddContacts {
    public List<wrapContact> contactList {get; set;}
    public Contact con {get;set;}
    public integer rowIndex {get;set;}


    public wrapperClassAddContacts () {
        rowIndex =0;
        contactList = new List<wrapContact>();
        Contact Cont = new Contact();
        contactList.add(new wrapContact(Cont));

    }
    Public PageReference addContact(){
        rowIndex += 1;
        Contact con = new Contact();
        contactList.add(new wrapContact(con));
        return null; 
    }

    Public void removeContact(){
        contactList.remove(rowIndex);
        rowIndex -= 1;   
    }


    public PageReference saveContact() {
        boolean emailexists = false;
        Map<String, Contact> contactEmailMap = new Map<String, Contact>();
        List<Contact> selectedContacts = new List<Contact>();
        list<string> minOnemaxTwoDSA = new list<string> ();

        for(wrapContact cCon: contactList) {
            if(cCon.selected == true){ 
                if(contactEmailMap.containsKey(cCon.con.Email)) {
                    cCon.con.Email.addError('Another Contact has same email address');
                    emailexists = true;
                } else {
                    contactEmailMap.put(cCon.con.Email,cCon.con);
                }
            }    
        }

        selectedContacts.addAll(contactEmailMap.values());

        if(emailexists ==false && selectedContacts.size() > 0  ){
            insert selectedContacts;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Contact Saved succesfully'));
            //contactList=null;
        }else{
            if(selectedContacts.isEmpty()){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Select atleast one contact'));
            }
        }        

        PageReference displayContactsPage = Page.DisplayContacts;
        displayContactsPage.setRedirect(false);
        return displayContactsPage ;   
    }

    public class wrapContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
        public Boolean isDSA {get;set;}

        public wrapContact(Contact c) {
            con = c;
            selected = false;

        }
    }
}

Generated PDF

enter image description here

1
  • Thank You for the detailed and informative explanation. Sep 25, 2019 at 16:50
1

user4947, I think the issue may be that your wrapperClassAddContacts constructor sets the contactList variable to an empty list, without checking it first. Try changing your constructor to something like this:

public wrapperClassAddContacts () {
      if(rowIndex == null) rowIndex =0;
      if(contactList == null){
          contactList = new List<wrapContact>();
          Contact Cont = new Contact();
          contactList.add(new wrapContact(Cont));
      } 
}

...and let me know if it works.

1
  • Thankyou @Aubry for replying, and for your suggestion. Sep 25, 2019 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .