Monday, June 24, 2013

Salesforce - Pass parameter value from one page to another page

Here i have provided a sample to understand how to pass parameter value from one page to another and retrieve it. I have used two pages named Books and SelectedBooks. Books page has a PageBlockTable it dispalyes only book names from the Book_c custom object. The book names are displayed as links using <apex:CommandLink>. When a user click the link the first page Book is navigate to second page SelectedBook with the parameter value that is passed using <apex:param>. The Second page then retrieves the parameter value and displays the book details in a PageBlockTable
Design code for Books page:

By default the PageBlockTable displays its value as OutputLabel but we need to click the value for navigation purpose. So here i used <apex:commandLink> to convert it as Link. Book name is passed as parameter using <apex:param>

<apex:page controller="BooksController">  
   <apex:form >  
   <apex:sectionHeader subtitle="All Books" title="Books"/>  
      <apex:pageBlock >  
       <apex:pageBlockTable value="{!allbooks}" var="a">  
           <apex:column headervalue="Name">  
                 <apex:commandLink value="{!a.Name}" action="/apex/SelectedBook?id={!a.Name}">  
                 <apex:param name="id" value="{!a.Name}"/>  
                 </apex:commandLink>  
           </apex:column>  
             </apex:pageBlockTable>  
        </apex:pageBlock>  
   </apex:form>  
 </apex:page>  

Apex Code for Books page controller:

Here the getAllBooks fuction retrive the values from the Book__c custom objects and bind it to the PageBlockTable

 public class BooksController
 {  
      public List<Book__c> allbooks;   
      public List<Book__c> getAllBooks()  
      {  
           if(allbooks==null)  
           {  
                allbooks=[SELECT Name,Book_Author__c,Book_ISBN__c from Book__c];  
           }  
      return allbooks;  
      }  
 }  

Apex Code for SelectedBook page controller:

Here in the constructor the parameter value is retrieved using its name. Using that value the getSbook function gets the specific book details and bind it to the PageBlockTable.

 public class SelectedBookController 
 {  
      public String selectedName {get;set;}  
      public Book__c sbook;  
      public SelectedBookController()  
      {  
           sbook=new Book__c();  
           selectedName = ApexPages.currentPage().getParameters().get('id');  
      }  
       
      public Book__c getSbook()  
      {  
           sbook=[Select Name,Book_ISBN__c,Book_Author__c from Book__c where Name=:selectedName];  
           return sbook;  
      } 
 } 

Design code for SelectedBook page:

<apex:page controller="SelectedBookController">  
 <apex:form >  
      <apex:pageBlock >  
           <apex:pageBlockTable value="{!sbook}" var="b">  
                <apex:column value="{!b.Name}"/>  
                <apex:column value="{!b.Book_Author__c }"/>  
               <apex:column value="{!b.Book_ISBN__c}"/>  
             </apex:pageBlockTable>  
      </apex:pageBlock>  
 </apex:form>  
 </apex:page>  

No comments:

Post a Comment