Question:

How to show the field names with their values in editable format in other vf page while clicking the command link

Everly: 2 weeks ago

i have created two vf pages 1.SaveRecWithCustomCtrl 2.AccRec in first page i am using two input fields and one save,cancel buttons to save and cancel the record.i am displaying few records in second page with command link.when i click on the command link it needs to be redirect to first page with name,rating values with editable format if we update record and click on save it again redirects to Accrec page.if we click on cancel button it should redirects to Accrec page. here i am using same controller for two pages.

Page1

<apex:page controller="SaveRecCustomCtrl">   
  <apex:form >
    <apex:pageBlock >
    <apex:outputLabel >Account Name
      <apex:inputField value="{!a.name}" label="Account Name"/></apex:outputLabel>
      <apex:inputfield value="{!a.rating}"/>
      <apex:commandButton value="Save" action="{!SaveRec}"/>
            <apex:commandButton value="cancel" action="{!cancel}"/>

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

Page2

<apex:page controller="SaveRecCustomCtrl"> 
  <apex:form >
  <Apex:pageBlock >
   <apex:pageBlockTable value="{!lstacc}" var="aa">

     <apex:column headerValue="Name">
      <apex:commandLink action="{!redir}">{!aa.name}</apex:commandLink>
     </apex:column>
     <apex:column value="{aa.rating}"/>
   </apex:pageBlockTable>
   </Apex:pageBlock>
  </apex:form>
</apex:page>

Controller

public class SaveRecCustomCtrl {

 public account a{get;set;}

 public list<account> lstAcc;

 public SaveRecCustomCtrl (){
 a = new account();
 }

 public list<account> getlstAcc(){

 lstacc = [select id,name from account];

 return lstacc;
 }

 public string id;
 public pagereference redir(){
 account aa=[select id,name from account where id =:apexpages.currentpage().getparameters().get('id')];
     pagereference pp=new pagereference('/apex/SaveRecWithCustomCtrl?id='+aa.id);

     return pp;
    //return page.SaveRecWithCustomCtrl;

 }

 Public pagereference SaveRec(){

  upsert a;
  pagereference p=new pagereference('/apex/AccRec?id='+a.id);
  return p;
 }

 Public pagereference Cancel(){

  //insert a;
  pagereference p=new pagereference('/apex/AccRec?id='+a.id);
  return p;
 }
}

Answer:
Carter: 2 weeks ago

You'll need to make the following fixes to your code.

in your controller: 1) change the pagereference p=new pagereference('/apex/AccRec?id='+a.id) to pagereference p=new pagereference('/apex/AccRec')

2) save the account you've lookup

in your AccRec page, add an to the command link to pass the id back

The working code is below:

Page AccRec

<apex:page controller="SaveRecCustomCtrl"> 
  <apex:form >
  <Apex:pageBlock >
   <apex:pageBlockTable value="{!lstacc}" var="aa">

     <apex:column headerValue="Name">
      <apex:commandLink action="{!redir}">{!aa.name}
          <apex:param name="id" value="{!aa.id}"/>
      </apex:commandLink>
     </apex:column>
     <apex:column value="{!aa.rating}"/>
   </apex:pageBlockTable>
   </Apex:pageBlock>
  </apex:form>
</apex:page>

Controller SaveRecCustomCtrl

public class SaveRecCustomCtrl {

 public account a{get;set;}

 public list<account> lstAcc;

 public SaveRecCustomCtrl (){
        a = new account();
 }

 public list<account> getlstAcc(){

 lstacc = [select id,name,rating from account];

 return lstacc;
 }

 public string id;
 public pagereference redir(){
     account aa=[select id,name,rating from account where id =:apexpages.currentpage().getparameters().get('id')];
     pagereference pp=new pagereference('/apex/SaveRecWithCustomCtrl?id='+aa.id);
     a=aa;
     return pp;
    //return page.SaveRecWithCustomCtrl;

 }

 Public pagereference SaveRec(){

  upsert a;
  //pagereference p=new pagereference('/apex/AccRec?id='+a.id);
  pagereference p=new pagereference('/apex/AccRec');
  return p;
 }

 Public pagereference Cancel(){

  //insert a;
  //pagereference p=new pagereference('/apex/AccRec?id='+a.id);
  pagereference p=new pagereference('/apex/AccRec');
  return p;
 }
}