+ Start a Discussion
Chitral ChaddaChitral Chadda 

code explanation

vf page with list of accounts ..when particular accnt name is selected it gives the name of the contacts in that particular account.
page
<apex:page standardController="Account" extensions="contactExt" showHeader="true" >

<apex:form >

<apex:pageBlock >

<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Name" for="accts"></apex:outputLabel>

<apex:selectList id="accts" value="{!selectedAcctId}" size="1" >

    <apex:selectOptions value="{!accts}"/>
    <apex:actionsupport event="onchange" action="{! getAccountContacts}" rerender="ContactDetail" />
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>


<apex:outputPanel id="ContactDetail">

<apex:repeat value="{!acctContacts}" var="contact">
<p>{!contact.FirstName & ' ' & contact.LastName}</p>
</apex:repeat>
       
</apex:outputPanel>
</apex:form>
    </apex:page>

controller:

public class contactExt{
   
public String selectedAcctId { get; set; }
    public List<Contact> acctContacts {get;set;}

    public contactExt(Apexpages.StandardController con){
               
    }
   
    public void getAccountContacts(){
       
        acctContacts = [Select Id, FirstName, LastName from Contact where AccountId = :selectedAcctId];
    }
   
    public List<selectOption> getaccts() {
        List<selectOption> options = new List<selectOption>();

        options.add(new selectOption('', '- None -'));

        for (Account account : [SELECT Id,  Name FROM Account]) {

            options.add(new selectOption(account.id, account.Name));

        }
        return options;
    }
   
   
}


whats is the use of :::

<apex:selectList id="accts" value="{!selectedAcctId}" size="1" >
taking id = "accts"and value="{! selectedAccId}" ..what is id and value holding
Best Answer chosen by Chitral Chadda
Virendra ChouhanVirendra Chouhan
Hi Chitral,

we use value="{!selectedAcctId}" to store our selected value from the List.
Means the value which we selected from pick list that value stored in SelectedAcctId variable.

All Answers

Pramod KumarPramod Kumar
Here id is unique id of the component. You can use this id in other component to refer this selectList.
Value is the controllers's variable whose value you want to access
Chitral ChaddaChitral Chadda
okay,i m new to this coding. bt what for are we using , value="{!selectedAcctId}" are we putting the accountid in it or accessing the alredy stored accnt id?? why we using value tag here?
Virendra ChouhanVirendra Chouhan
Hi Chitral,

we use value="{!selectedAcctId}" to store our selected value from the List.
Means the value which we selected from pick list that value stored in SelectedAcctId variable.
This was selected as the best answer
Pramod KumarPramod Kumar
Value tag bind the component with controller's variable so whatever you will type in, it will stored that in controller variable and displayed on page. It does both putting and accessing. If value is in the controller variable then will display that value and if you change the value it will set that value in controller field.
Chitral ChaddaChitral Chadda
perfect .
means in the vf page suppose we select accnt name abcd so it id is stored in SelectedAcctId  and  
it will loop inside theacctContacts   based on that SelectedAcctId from the picklist .
 
public void getAccountContacts(){
      
        acctContacts = [Select Id, FirstName, LastName from Contact where AccountId = :selectedAcctId];
    }
Pramod KumarPramod Kumar
Correct.
Chitral ChaddaChitral Chadda
n for "id" tag in select list  it will display name of all the account having unique id which it will check in accts
<apex:selectList id="accts" value="{!selectedAcctId}" size="1" >
Chitral ChaddaChitral Chadda

<apex:actionsupport event="onchange" action="{! getAccountContacts}" rerender="ContactDetail" />
it will go to getAccountContacts then ,

<apex:repeat value="{!acctContacts}" var="contact">
<p>{!contact.FirstName & ' ' & contact.LastName}</p>
</apex:repeat>


it will loop in acctContacts  and for all the contact found for a particular id , it will save it to the list :
 public List<Contact> acctContacts {get;set;}

 nd  we will print it thru 
var="contact"
<p>{!contact.FirstName & ' ' & contact.LastName}</p>


is this the functioning?