+ Start a Discussion
Satya413Satya413 

Display Selected Account Record Details in VF Page

Hi, 

I am trying to display the account details ( name, address, phone ) for the account selected in picklist. I have written the code for picklist as below. Any help is appreciated. 

<apex:page Controller="accountcontroller" showHeader="false">
    <apex:form>
        <apex:pageBlock title="Display Account Details: ">
            
            <apex:pageBlockSection>
            <apex:outputLabel value="Account Name: " />
            
            <apex:selectlist value="{!accid}" size="1" >
                <apex:selectOptions value="{!accountnames}" />
                <apex:actionSupport event="onchange" rerender = "display"/>
            </apex:selectlist>
            </apex:pageBlockSection>
            
        <apex:pageBlockSection title = 'Account Details'>
            
        </apex:pageBlockSection>    
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller 

public class accountcontroller {

    public string accid {get; set;}
    
    public list<selectoption> getaccountnames() {
        
        list<selectoption> accoptions = new list<selectoption>();
        for (account acc : [select id, name from account]){
            accoptions.add(new selectoption(acc.id, acc.name));
        }  
        return accoptions;
    } 
}

Thank you,
Satya
Best Answer chosen by Satya413
Chamil MadusankaChamil Madusanka
Try this code
 
<apex:page Controller="accountcontroller" >
    <apex:form >
        <apex:pageBlock title="Display Account Details: ">
            
            <apex:pageBlockSection >
            <apex:outputLabel value="Account Name: " />
            
            <apex:selectlist value="{!accid}" size="1" >
                <apex:selectOptions value="{!accountnames}" />
                <apex:actionSupport action="{!getDetails}" event="onchange" rerender="display"/>
            </apex:selectlist>
            </apex:pageBlockSection>
            
        <apex:pageBlockSection id="display" title="Account Details">
            <apex:outputField value="{!SelectedAcc.Name}"/>
            <apex:outputField value="{!SelectedAcc.Phone}"/>
            
        </apex:pageBlockSection>    
            
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class accountcontroller {

    public string accid {get; set;}
    public Account SelectedAcc{get;set;}
    
    public list<selectoption> getaccountnames() {
        
        list<selectoption> accoptions = new list<selectoption>();
        for (account acc : [select id, name from account]){
            accoptions.add(new selectoption(acc.id, acc.name));
        }  
        return accoptions;
    } 
    
    public void  getDetails() {
        
        SelectedAcc = new Account();
        SelectedAcc = [SELECT id, Name, Phone FROM Account WHERE id=:accid ];
        
    }
}


If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.
 

All Answers

Chamil MadusankaChamil Madusanka
Try this code
 
<apex:page Controller="accountcontroller" >
    <apex:form >
        <apex:pageBlock title="Display Account Details: ">
            
            <apex:pageBlockSection >
            <apex:outputLabel value="Account Name: " />
            
            <apex:selectlist value="{!accid}" size="1" >
                <apex:selectOptions value="{!accountnames}" />
                <apex:actionSupport action="{!getDetails}" event="onchange" rerender="display"/>
            </apex:selectlist>
            </apex:pageBlockSection>
            
        <apex:pageBlockSection id="display" title="Account Details">
            <apex:outputField value="{!SelectedAcc.Name}"/>
            <apex:outputField value="{!SelectedAcc.Phone}"/>
            
        </apex:pageBlockSection>    
            
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class accountcontroller {

    public string accid {get; set;}
    public Account SelectedAcc{get;set;}
    
    public list<selectoption> getaccountnames() {
        
        list<selectoption> accoptions = new list<selectoption>();
        for (account acc : [select id, name from account]){
            accoptions.add(new selectoption(acc.id, acc.name));
        }  
        return accoptions;
    } 
    
    public void  getDetails() {
        
        SelectedAcc = new Account();
        SelectedAcc = [SELECT id, Name, Phone FROM Account WHERE id=:accid ];
        
    }
}


If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.
 
This was selected as the best answer
Satya413Satya413
Hi Chamil,

Thanks for your help. I am able to get the account details now. 

Thank you,
Satya
Tanmoy DashTanmoy Dash
Hi Chamil 
I tried your code and I got an error Constructor not defined. What should I do 
Tanmoy DashTanmoy Dash
I Have a custom object by the name of Equipments__c. It has a picklist field Equipmet_Category__c. I need to show records based on the selected picklist Equipment_Category__c. 
 
Tanmoy DashTanmoy Dash
I Have a custom object by the name of Equipments__c. It has a picklist field Equipmet_Category__c. I need to show records based on the selected picklist Equipment_Category__c.