0

I have two VFpages

In 1st VF Page:

  1. I have a 3 input field and I am entering data

  2. I have a button(Standard/custom).

  3. When I click on this button, It should redirect to another page(2nd VF Page) with the same fields with output fields along with the data I have entered

    <apex:page standardController="Contact" >
    <apex:form >
    
    <script>
     function OpenVfpageConfrim(){
    var confirmFlag =confirm('Do you want to open a new visualforce page?');
    if(confirmFlag == true)
       window.open('/apex/anothervfpage');
       }</script>
    <apex:pageBlock title="Edit Contact">
    
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!contact.FirstName}"/>
            <apex:inputField value="{!contact.LastName}"/>
            <apex:inputField value="{!contact.Birthdate}"/>
        </apex:pageBlockSection>
    <apex:pageBlockButtons >
        <apex:commandButton value="Open vf Page" onclick="OpenVfpageConfrim()"/>
    </apex:pageBlockButtons>
    </apex:pageBlock>
    

    enter image description here

In 2nd VF page

  1. It should display the same 1st VF Page with output fields
  2. There should be a button to SAVE This data into the database

     <apex:page Controller="ContactControllerr" >
     <apex:form >
    
        <apex:pageBlock title="Edit Contact">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
        </apex:pageBlockButtons>
      <apex:pageBlockTable value="{!samepage}" var="c">
      <apex:column headerValue="First Name">
      <apex:outputField value="{!c.Firstname}"/>
      </apex:column>
    
      <apex:column headerValue="Last Name">
      <apex:outputField value="{!c.Lastname}"/>
      </apex:column>
    
      <apex:column headerValue="Birthdate">
      <apex:outputField value="{!c.Birthdate}"/>
      </apex:column>
      </apex:pageBlockTable>
    </apex:pageBlock>
    

    public with sharing class ContactControllerr {
    public Contact c { get; set; }
    public List<Contact> samepage { get; set; }
    public ContactControllerr(){
           c=new Contact();
       }
         public PageReference save() {
          insert c;  
      samepage= [select id,FirstName,LastName,Email,Birthdate from Contact 
       where id=:c.id];
    
    return null;
        }
       }
    

    enter image description here

    Data is not coming and saving into the database?

Can anyone help me to solve this issue?

2
  • 1
    Do you really need two pages for this? Some conditional rendering and some simple re-rendering may be enough to accomplish this. Also, how much do you know about the "viewstate" of a visualforce page? Splitting this across two separate pages is possible, but I think the single-page + rerender approach would be faster/easier. – Derek F Jul 24 '18 at 20:25
  • Rerender approach means I am not sure about Derek. Can you please share any example or suggest me how to do that? Thanks in advance – user51181 Jul 24 '18 at 20:30
0

This is a very simple example on how to implement this kind of functionality.

VF Page

<apex:page standardController="Contact" extensions="TestCreateContactCont">
    <apex:form id="theForm">
        <apex:pageBlock title="Edit Contact" rendered="{!editPage}">
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!ct.FirstName}"/>
                <apex:inputField value="{!ct.LastName}"/>
                <apex:inputField value="{!ct.Birthdate}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Next" action="{!next}" reRender="theForm"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>

        <apex:pageBlock title="Edit Contact" rendered="{!!editPage}">
            <apex:pageBlockTable value="{!ct}" var="c">
                <apex:column headerValue="First Name">
                    <apex:outputField value="{!c.Firstname}"/>
                </apex:column>

                <apex:column headerValue="Last Name">
                    <apex:outputField value="{!c.Lastname}"/>
                </apex:column>

                <apex:column headerValue="Birthdate">
                    <apex:outputField value="{!c.Birthdate}"/>
                </apex:column>
            </apex:pageBlockTable>

             <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page> 

Apex Class Extention

public class TestCreateContactCont {
    public Contact ct{get;set;}
    public Boolean editPage {get;set;}

    public TestCreateContactCont(ApexPages.StandardController stdController){
        ct = (Contact)stdController.getRecord();
        editPage = true;
    }

    public void next() {
        editPage = false;
    }

    public PageReference save() {
        insert ct;
        return new PageReference('/'+ct.Id);
    }
}

As @Derek F mentioned you can achieve this without having to use two controllers and two pages. In a complex scenario where if you must use two controllers and two pages, I'd suggest you share the same controller between vf pages; which enables you to easily pass values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy