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
SF7SF7 

Cretae Parent and Child Records using 2 VF Pages

Hi ,

I have 2 obkects called Client Discovery and Client Site. I am using VF pages for both these objects . And user needs to fill in the first page and then click save and then he has to be redirected to the second VF page. And when user is in 2nd Vf page the Lookup field has to be filled with Parent Name(ID). [ Like we have account name pre filled while creating contacts ]
VISUALFORCE PAGE # 1

<apex:page standardController="Client_Discovery__c" standardStylesheets="true"  >
<apex:sectionHeader title="Client Discovery " subtitle="{!Client_Discovery__c.name}"/>
<apex:form >
<apex:pageBlock title="Large Client Sales Questions" mode="edit">

<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Save & New" action="{!save}" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>

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

<apex:pageBlockSection title="Client Needs" columns="1">

<apex:panelGrid columns="2" style="white-space:nowrap;" >
<Apex:outputlabel Value="What are the Client's pain points?" for="Question1"  style="font-size:13px;font-family:arial;"/>
<apex:inputField value="{!Client_Discovery__c.What_are_the_client_s_pain_points__c}" id="Question1" required="false"  />
<Apex:outputlabel Value="List Other Client needs here" for="Question1-1"  style="font-size:13px;font-family:arial;"/>
<apex:inputField value="{!Client_Discovery__c.List_Other_client_needs_here__c}" id="Question1-1" required="false"  />
</apex:panelGrid>
</apex:pageBlockSection>

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

I have not written a Controller yet for this page and currently using Satndard controller only . I need to have a controller to get the Save and then redirect to the 2nd Vf page .
 
Visual Force Page 2

<apex:page controller="ManageListController2" sidebar="false">
 <apex:form >
 <apex:pageBlock title="Client Sites for Client Discovery">
 <apex:pageBlockSection title="Client Site Details">
 
     <apex:pageblockSectionItem >
         <apex:outputLabel value="Number of Client Sites" for="noofSites"></apex:outputLabel>
         <apex:inputText id="noofSites" value="{!noOfDays}"/>
     </apex:pageblockSectionItem>
     <apex:pageBlockSectionItem >
         <apex:commandButton value="Add Rows" action="{!addRows}" rerender="wtable">
             <apex:param name="addCount" value="{!noOfDays}" assignTo="{!addCount}"/>
         </apex:commandButton>
     </apex:pageBlockSectionItem>
 </apex:pageBlockSection>
   <apex:pageBlockSection >
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable" columns="11" rows="20">
      <tr>
         <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Client Site Name">
            <apex:inputField value="{!wrapper.acc.Client_Site_Name__c}"/>
         </apex:column>
         <apex:column headerValue="City">
            <apex:inputField value="{!wrapper.acc.City__c}"/>
         </apex:column>
         <apex:column headerValue="State">
            <apex:inputField value="{!wrapper.acc.State__c}"/>
         </apex:column>
         </tr>
         <apex:column headerValue="Headcount">
            <apex:inputField value="{!wrapper.acc.Head_count__c}"/>
         </apex:column>
         <apex:column headerValue="Number of Shifts">
            <apex:inputField value="{!wrapper.acc.Number_of_Shifts__c}"/>
         </apex:column>
         <apex:column headerValue="Job Titles">
            <apex:inputField value="{!wrapper.acc.Job_Titles__c}"/>
         </apex:column>
         
         <apex:column headerValue="Number of On-Site Managers">
            <apex:inputField value="{!wrapper.acc.Number_of_On_Site_Managers__c}"/>
         </apex:column>
         <apex:column headerValue="Union or Non union">
            <apex:inputField value="{!wrapper.acc.Union_or_Non_Union__c}"/>
         </apex:column>
         <apex:column headerValue="Number of suppliers to manage">
            <apex:inputField value="{!wrapper.acc.Number_of_Team_Leads__c}"/>
         </apex:column>
         <apex:column headerValue="Questionnaire">
            <apex:inputField value="{!wrapper.acc.Client_Discovery__c}"/>
         </apex:column>
         <apex:column headerValue="Action">
            <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
            </apex:commandButton>
         </apex:column>
      </apex:pageBlockTable>
      </apex:pageBlockSection>
    <!--  <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/>
      </apex:commandButton> -->
      
      <apex:commandButton value="Save" action="{!save}"/>
   </apex:pageBlock>
 </apex:form>
</apex:page>
 
public class ManageListController2
{  

 public String noOfDays { get; set; }
 public List<AccountWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=1;
  public Client_Discovery__C opp{get; set;}
  
 public ManageListController2()
 {
  wrappers=new List<AccountWrapper>();
  for (Integer idx=0; idx<0; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public void delWrapper()
 {
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
   
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
 }
  
 public void addRows()
 {

 addCount = Integer.valueOf(noOfDays);
  System.Debug('addCount ::: '+addCount);
  for (Integer idx=1; idx<=addCount; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public PageReference save()
 {
  List<Client_Site__C> accs=new List<Client_Site__C>( );
  
  for (AccountWrapper wrap : wrappers)
  {
   accs.add(wrap.acc);
  }
   
  insert accs;
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('Client_Site__c').getDescribe().getKeyPrefix() + '/o');
 }
  
 public class AccountWrapper
 {
  public Client_Site__c acc {get; private set;}
  public Integer ident {get; private set;}
   
  public AccountWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new Client_Site__C();
  }
 }
}

 
Vivek_PatelVivek_Patel
Hi Akhil

As you mentioned you have not created controller for the first page, create a controller for the first page and redirect to the second page using following code after inserting the first record.

Return following page reference from the custom save method from you new controller, this will redirect you to the next page.

PageReference newPageRef = new PageReference('/apex/VFPage2?client_desc='+client_desc.Id);


In the controller of the second page read the query string parameter, the id of the parent record, query the record and assign it to the variable on the second page's controller. Now if you use any field from the parent record on the page, they will be populated with correct value.

ApexPages.currentPage().getParameters().get('client_desc');

Regards,
Vivek Patel

Please like or mark this as best answer if this answers your query to help others find better answers and improve the quality of the developer forum.