passing parameters to visualforce component

When we call a visualforce component we generally need to pass parameters to that component to get it working.

Let us see how we can pass parameters to a visualforce component.

In the following example i have demonstrated passing following to the vf component:

2.List of strings
3.List of records (sObject/any object)

Visualforce component

<apex:component controller="VisualForceComponentController">
  <!-- Receive string variable input here -->
  <apex:attribute name="InputString" description="Received varaible of data type string"  assignTo="{!Received_String}" type="String" required="true"/>
  <!-- Receive List of strings here -->
  <apex:attribute name="inputList_string" description="Received list of strings" type="string[]" assignTo="{!Received_List_string}" required="false"/>
  <!-- Receive List of sObject Records here -->
  <apex:attribute name="InputList_sObje" description="Received list of sObject records" type="sObject[]" assignTo="{!Received_List_sObject}" required="false"/>
  <!-- Print your received string here -->
 <b> Received Input string is  : </b><apex:outputtext value="{!Received_String}"/>
 <apex:pageblock >
      <!-- Print your List of strings here -->
      <apex:pageblocktable value="{!Received_List_string}" var="str" >
          <apex:outputtext value="{!str}"/>
       <!-- Print your List of sObject Records here -->
      <apex:pageblocktable value="{!AccList}" var="sObjRec">
           <apex:repeat value="{!FieldList}" var="fl">
              <apex:column value="{!sObjRec[fl]}"/>
   <apex:commandButton value="Call Controller method" action="{!Access_paramtrs_In_Controlr}"/>


Public Class VisualForceComponentController{
    Public string Received_String {get;set;}
    Public List<string> Received_List_string{get;set;}
    Public List<sobject> Received_List_sObject{get;set;}
    Public List<sobject> Display_sObj_List{get;set;}
    Public List<string> FieldList{get;set;}
    Public VisualForceComponentController(){
         System.debug('--@@Check the received parameters in constructor@@--'+ Received_String+Received_List_string+Received_List_sObject );
         FieldList = new List<string>();
   Public void  Access_paramtrs_In_Controlr(){
        System.debug('--@@Check the received string in Method@@--'+ Received_String);
        System.debug('--@@Check the received strin List in Method@@--'+Received_List_string);
        System.debug('--@@Check the received sObject List in Method@@--'+Received_List_sObject );
   Public List<sobject> getAccList(){
      Display_sObj_List =[select name,accountNumber,description from account where id IN: Received_List_sObject];
      return Display_sObj_List ;

The received parameter should be assigned to a similar data type variable. For this use attribute: "assignto"
Its important to mention the data type when you receive your parameter in component. 

<apex:attribute name="InputString" description="Received variable of data type string"  assignTo="{!Received_String}" type="String" required="true"/>

A simple string will be "string", list of strings would be string[] and sObject List would be sObject[].

Note that the received parameters are not available in the constructor hence 1st debug statement in constructor will show null values.However, received parameters can be accessed in a method as demonstrated in the example.

Calling visualforce component in a visualforce page.

Example visualforce page

<apex:page controller="ExampleController">
      <c:VisualForceComponent InputString="Test string" inputList_string="{!Country_List}" InputList_sObje="{!Account_List}" />

Example controller
Public with sharing class ExampleController {
   Public List<account> Account_List{get;set;}
   Public List<string> Country_List{get;set;}
    Public ExampleController (){
      Account_List = New list<account>();
      Account_List = [Select name,accountNumber from account limit 5];
      Country_List = New List<string>();
      Country_List.add('United Kingdom');
      Country_List.add('United States');


  1. How to pass List to the Visualforce Custom Component

  2. I mean how to pass List of record Ids to the Visualforce Component

    1. i have demonstrated passing list of strings above, id's can be stored as strings, so you can try in same manner as demonstrated above... if you want to pass list of sobject then mention it as sobject[], eg. account[]

    2. Passing list of records is not working though I write the code same like your's. Could you please help me?