Tuesday 31 March 2015

How to Pass ID in Visualforce Pages

Apex Controller:-

public class Teamlist {
  
    
    public ApexPages.StandardSetController setTeam {
        get {
            if(setTeam == null) {
                setTeam = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select id, name from Team__c]));
            }
            return setTeam;
        }
        set;
    }
    
     public List<Team__c> getTeams() {
         return (List<Team__c>) setTeam.getRecords();
       }  
         
    public PageReference Go() {
      Id id = System.currentPageReference().getParameters().get('id');
      PageReference nextpage = new PageReference('/apex/CustomTeam?id='+id);
                return nextpage;
    }
  }

VisualForce Page:-

<apex:page controller="Teamlist" showHeader="false" title="false"> 
  <apex:form >
   <apex:tabPanel switchType="client" selectedTab="TabDetails" tabClass="activetab" inactiveTabClass="inactivetab">
        <apex:tab label="TEAM">
            <apex:pageBlock title="TEAMLIST">
                <apex:pageBlockTable value="{!teams}" var="t">
                    <apex:column headerValue="TEAM NAME">
                       <apex:outputPanel >
                            <apex:commandLink action="{!Go}">
                               {!t.name}<apex:param name="id" value="{!t.id}"/>
                            </apex:commandLink>
                       </apex:outputPanel>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:tab>
   </apex:tabPanel>
  </apex:form>
</apex:page>



Here is a visualforce page containing a list of teams. What we want is after clicking a particular team name, it will go to a page where the details are displayed of that corresponding team. Here we are retrieving the team id using <apex:param name="id" value="{!t.id}"/> within commandLink where we defined the action.
Then we can observe that we defined that function in the apex controller for getting that id by System.currentPageReference().getParameters().get('id'); After that it is referenced in a different page with that id. It may be simple,but as a beginner I think it is an important thing to know.

No comments:

Post a Comment