+ Start a Discussion
Abhishek PurohitAbhishek Purohit 

I have 2 vf pages and 1 custom controller want to add the record,save it on the visualforce page and database and redirect again to the 1st page please help .

this is vf3 page
<apex:page controller="OppControl">
 <apex:form >
  <apex:pageBlock title="Search The Opportunities">
   Name: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:inputText value="{!key}"/>
     <apex:commandButton value="Go" action="{!search}"/>
        <apex:commandButton value="Add Opportunity " action="{!add}"/>
       <apex:pageBlockTable value="{!myop}" var="my">
       <apex:column ><apex:outputLink value="/{!my.id}">"{!my.Name}"</apex:outputLink></apex:column>
        
         <apex:column value="{!my.StageName}"/>
         <apex:column value="{!my.Amount}"/>
         <apex:column value="{!my.CloseDate}"/>     
      </apex:pageBlockTable>
  </apex:pageBlock>  
 </apex:form>
</apex:page>





This one redirects from the add opportunity button

<apex:page controller="OppControl">
  <apex:form >
  
  <apex:pageBlock title="The New Opportunity">
    <apex:pageBlockSection title="Section One"> 
     <apex:outputText value="Enter The Name"></apex:outputText>
     <apex:inputText />
    </apex:pageBlockSection>
    
    
  <apex:pageBlockSection title="Section Two"> 
     <apex:outputText value="Enter The Amount"></apex:outputText>
     <apex:inputText />
    </apex:pageBlockSection>
  
  <apex:pageBlockSection title="Section Three"> 
     <apex:outputText value="Enter The Stage Name"></apex:outputText>
     <apex:inputText />
    </apex:pageBlockSection>
  
  <apex:pageBlockSection title="Section Four"> 
     <apex:outputText value="Enter The Closed Date"></apex:outputText>
    <apex:inputText />
    
    <apex:commandButton value="Save it" action="{!save}"/>
    </apex:pageBlockSection>
  </apex:pageblock>
  </apex:form>
</apex:page>




This is my Custom Controller

public class OppControl{

    

    String key;
    List<Opportunity> myop;
    String SearchQuery{get;set;}
    public String getkey(){
    return key;
    }
  public OppControl(){
  myop=[Select Name,Amount,StageName,CloseDate From Opportunity];
  }
    public List<Opportunity> getmyop(){
    return myop;
    }

   public void setkey(String ip){
     key=ip;
   }

   public PageReference search(){
    SearchQuery='Select Name,StageName,Amount,CloseDate From Opportunity where Name like '+'\''+key+'%'+'\''; 
    myop=database.query(SearchQuery);
   return null;
}   
   public PageReference add(){
   return Page.vf4;
   
   
  
  } 
   public PageReference save(){
   return Page.vf3;
   }
   
}


Now I want to add the record like for real in the database using this controller and also after it has been saved on that vf4 page should be able to show the name stagename closedate amount on the vf3 page

 
Best Answer chosen by Abhishek Purohit
Dayakar.DDayakar.D
Hi Abhishek Purohit,

Please find below code it will work.
 
<apex:page controller="OppControl">
 <apex:form >
  <apex:pageBlock title="Search The Opportunities">
   Name: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:inputText value="{!key}"/>
     <apex:commandButton value="Go" action="{!search}"/>
        <apex:commandButton value="Add Opportunity " action="{!add}"/>
       <apex:pageBlockTable value="{!myop}" var="my">
       <apex:column ><apex:outputLink value="/{!my.id}">"{!my.Name}"</apex:outputLink></apex:column>
        
         <apex:column value="{!my.StageName}"/>
         <apex:column value="{!my.Amount}"/>
         <apex:column value="{!my.CloseDate}"/>     
      </apex:pageBlockTable>
  </apex:pageBlock>  
 </apex:form>
</apex:page>

////////////////////////////////////////////////////////////////////////////////////////////////////

<apex:page controller="OppControl">
  <apex:form >
  
  <apex:pageBlock title="The New Opportunity">
    <apex:pageBlockSection title="Section One"> 
     <apex:outputText value="Enter The Name"></apex:outputText>
     <apex:inputText value="{!name}"/>
    </apex:pageBlockSection>
    
    
  <apex:pageBlockSection title="Section Two"> 
     <apex:outputText value="Enter The Amount"></apex:outputText>
     <apex:inputText value="{!amount}"/>
    </apex:pageBlockSection>
  
  <apex:pageBlockSection title="Section Three"> 
     <apex:outputText value="Enter The Stage Name"></apex:outputText>
     <apex:inputText value="{!stageName}"/>
    </apex:pageBlockSection>
  
  <apex:pageBlockSection title="Section Four"> 
     <apex:outputText value="Enter The Closed Date"></apex:outputText>
    <apex:inputText value="{!closedDate}" />
    
    <apex:commandButton value="Save it" action="{!save}"/>
    </apex:pageBlockSection>
  </apex:pageblock>
  </apex:form>
</apex:page>




public class OppControl{
    public string name {set;get;}
    public integer amount {set;get;}
    public string stageName {set;get;}
    public date closedDate {set;get;}

    

    String key;
    List<Opportunity> myop;
    String SearchQuery{get;set;}
    public String getkey(){
    return key;
    }
  public OppControl(){
  myop=[Select Name,Amount,StageName,CloseDate From Opportunity order by CreatedDate DESC];
  }
    public List<Opportunity> getmyop(){
    return myop;
    }

   public void setkey(String ip){
     key=ip;
   }

   public PageReference search(){
    SearchQuery='Select Name,StageName,Amount,CloseDate From Opportunity where Name like '+'\''+key+'%'+'\''; 
    myop=database.query(SearchQuery);
   return null;
}   
   public PageReference add(){
   return Page.vf2;
   
   
  
  } 
   public PageReference save(){
       
       Opportunity OppObj=new Opportunity();
       oppObj.Name=name;
       oppObj.StageName=stageName;
       oppObj.CloseDate=closedDate;
       oppObj.Amount=amount;
       insert oppObj;
   return Page.vf1;
   }
  
   
}

Please let me know, if it helps you.

Best Regards,
Dayakar.D

All Answers

Virendra ChouhanVirendra Chouhan
Hi Abhishek,

You must have to bind your input fields with your object instance to save record into database. First create an instace in your controller. like
public Opportunity oppObj = new Opportunity();
and use it into your vf page:
<apex:inputField id="opportunityName" value="{!oppObj.name}"/>
Then in save method do DML on oppObj instance.
insert oppObj;

you have aleardy written the code to rediect it to first vf page:
return Page.vf3;

Please look into below link for refrence : how to create Wizard in vf page:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_wizard.htm

 
Abhishek PurohitAbhishek Purohit
Can you please send me entire code? Still not able to get u
Dayakar.DDayakar.D
Hi Abhishek Purohit,

Please find below code it will work.
 
<apex:page controller="OppControl">
 <apex:form >
  <apex:pageBlock title="Search The Opportunities">
   Name: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:inputText value="{!key}"/>
     <apex:commandButton value="Go" action="{!search}"/>
        <apex:commandButton value="Add Opportunity " action="{!add}"/>
       <apex:pageBlockTable value="{!myop}" var="my">
       <apex:column ><apex:outputLink value="/{!my.id}">"{!my.Name}"</apex:outputLink></apex:column>
        
         <apex:column value="{!my.StageName}"/>
         <apex:column value="{!my.Amount}"/>
         <apex:column value="{!my.CloseDate}"/>     
      </apex:pageBlockTable>
  </apex:pageBlock>  
 </apex:form>
</apex:page>

////////////////////////////////////////////////////////////////////////////////////////////////////

<apex:page controller="OppControl">
  <apex:form >
  
  <apex:pageBlock title="The New Opportunity">
    <apex:pageBlockSection title="Section One"> 
     <apex:outputText value="Enter The Name"></apex:outputText>
     <apex:inputText value="{!name}"/>
    </apex:pageBlockSection>
    
    
  <apex:pageBlockSection title="Section Two"> 
     <apex:outputText value="Enter The Amount"></apex:outputText>
     <apex:inputText value="{!amount}"/>
    </apex:pageBlockSection>
  
  <apex:pageBlockSection title="Section Three"> 
     <apex:outputText value="Enter The Stage Name"></apex:outputText>
     <apex:inputText value="{!stageName}"/>
    </apex:pageBlockSection>
  
  <apex:pageBlockSection title="Section Four"> 
     <apex:outputText value="Enter The Closed Date"></apex:outputText>
    <apex:inputText value="{!closedDate}" />
    
    <apex:commandButton value="Save it" action="{!save}"/>
    </apex:pageBlockSection>
  </apex:pageblock>
  </apex:form>
</apex:page>




public class OppControl{
    public string name {set;get;}
    public integer amount {set;get;}
    public string stageName {set;get;}
    public date closedDate {set;get;}

    

    String key;
    List<Opportunity> myop;
    String SearchQuery{get;set;}
    public String getkey(){
    return key;
    }
  public OppControl(){
  myop=[Select Name,Amount,StageName,CloseDate From Opportunity order by CreatedDate DESC];
  }
    public List<Opportunity> getmyop(){
    return myop;
    }

   public void setkey(String ip){
     key=ip;
   }

   public PageReference search(){
    SearchQuery='Select Name,StageName,Amount,CloseDate From Opportunity where Name like '+'\''+key+'%'+'\''; 
    myop=database.query(SearchQuery);
   return null;
}   
   public PageReference add(){
   return Page.vf2;
   
   
  
  } 
   public PageReference save(){
       
       Opportunity OppObj=new Opportunity();
       oppObj.Name=name;
       oppObj.StageName=stageName;
       oppObj.CloseDate=closedDate;
       oppObj.Amount=amount;
       insert oppObj;
   return Page.vf1;
   }
  
   
}

Please let me know, if it helps you.

Best Regards,
Dayakar.D
This was selected as the best answer
Abhishek PurohitAbhishek Purohit
Thank u sir!
Now for the inline editing and the deletion operation how can i use it????also I want to take the stagename in picklist and the closed date in the date table format??the date box 
Abhishek PurohitAbhishek Purohit
Also the Checkbox should be there for selecting the opportunities for the deletion purpose