function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vamshi(Nani)vamshi(Nani) 

How to solve this select option

I have a visual force page, On that page I created a pick list.. I am fetching the values of account. Now when I selected
One record related..Id  should display how can I do that?

The code as follows.

<apex:page controller="selectlist">
<apex:form >
<apex:selectList value="{!sv}" size="1" rendered="true" onselect="output">
<apex:selectOptions value="{!value}">
</apex:selectOptions>
</apex:selectList>
</apex:form>
<apex:outputPanel id="output">
<apex:outputLabel value="{!Iddisplay}">
</apex:outputLabel>
</apex:outputPanel>

</apex:page>

 

 

 

------------------------------------------------ APEX CLASS------------------------------------

public class selectlist
{
public String sv { get; set; }
public String getIddisplay()
{
return null;
}


public List<SelectOption> getValue()
{
List<SelectOption> option = new List<SelectOption>();
option.add(new selectoption('none','---Select--'));
for(Account ac: [Select Id,name From Account limit 5])
{
option.add(new SelectOption(ac.id,ac.name));

}
return option;
}



}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi vamshi,

 

You have to use actionFunction tag using javascript to call controller method to display selected recordId.

 

see below example.

 

visualforce Page:

<apex:page controller="selectlist">
    <apex:form id="frm">
    <script>
        function Iddisplay(sel){          
            callgetIddisplay(sel.value);
        }
    </script>
    <apex:actionFunction name="callgetIddisplay" action="{!getIddisplay}" reRender="frm">
        <apex:param value="{!sv}" name="recId"/>
    </apex:actionFunction >
        <apex:selectList value="{!sv}" size="1" rendered="true" onchange="Iddisplay(this)">            
            <apex:selectOptions value="{!value}">
            </apex:selectOptions>
        </apex:selectList>
        <br/><br/>
        <apex:OutputLabel value="selected Account ID : {!strSelectedAccID}"></apex:OutputLabel> 
    </apex:form>
    <apex:outputPanel id="output">
        <apex:outputLabel value="{!Iddisplay}">
        </apex:outputLabel>
    </apex:outputPanel>
</apex:page>

 

 

Apex Class:

public class selectlist{
    public String sv {get; set;}
    public String strSelectedAccID {get; set;}
    public String getIddisplay(){
        strSelectedAccID = apexpages.currentpage().getparameters().get('recId');
        return null;
    }
    public List<SelectOption> getValue(){
        List<SelectOption> option = new List<SelectOption>();
        option.add(new selectoption('none','---Select--'));
        for(Account ac: [Select Id,name From Account limit 5]){
            option.add(new SelectOption(ac.id,ac.name));        
        }
        return option;
    }
}

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90

Hi vamshi,

 

You have to use actionFunction tag using javascript to call controller method to display selected recordId.

 

see below example.

 

visualforce Page:

<apex:page controller="selectlist">
    <apex:form id="frm">
    <script>
        function Iddisplay(sel){          
            callgetIddisplay(sel.value);
        }
    </script>
    <apex:actionFunction name="callgetIddisplay" action="{!getIddisplay}" reRender="frm">
        <apex:param value="{!sv}" name="recId"/>
    </apex:actionFunction >
        <apex:selectList value="{!sv}" size="1" rendered="true" onchange="Iddisplay(this)">            
            <apex:selectOptions value="{!value}">
            </apex:selectOptions>
        </apex:selectList>
        <br/><br/>
        <apex:OutputLabel value="selected Account ID : {!strSelectedAccID}"></apex:OutputLabel> 
    </apex:form>
    <apex:outputPanel id="output">
        <apex:outputLabel value="{!Iddisplay}">
        </apex:outputLabel>
    </apex:outputPanel>
</apex:page>

 

 

Apex Class:

public class selectlist{
    public String sv {get; set;}
    public String strSelectedAccID {get; set;}
    public String getIddisplay(){
        strSelectedAccID = apexpages.currentpage().getparameters().get('recId');
        return null;
    }
    public List<SelectOption> getValue(){
        List<SelectOption> option = new List<SelectOption>();
        option.add(new selectoption('none','---Select--'));
        for(Account ac: [Select Id,name From Account limit 5]){
            option.add(new SelectOption(ac.id,ac.name));        
        }
        return option;
    }
}

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

This was selected as the best answer
GlynAGlynA

You can also do this without an ActionFunction, just use ActionSupport:

 

<apex:page controller="SelectList">

<apex:form >
	<apex:selectList value="{!sv}" size="1" rendered="true">
		<apex:actionSupport event="onchange" rerender="output"/>
		<apex:selectOptions value="{!value}"/>
	</apex:selectList>
</apex:form>

<apex:outputPanel id="output">
	<apex:outputText value="{!sv}"/>
</apex:outputPanel>

</apex:page>

 

public with sharing class SelectList
{
	public String sv { get; set; }

	public List<SelectOption> value
	{
		get
		{
			List<SelectOption> options = new List<SelectOption>();
			options.add( new SelectOption( 'none', '--Select--' ) );
			for ( Account ac: [SELECT Id, Name FROM Account LIMIT 5] )
			{
				options.add( new SelectOption( ac.id, ac.name ) );
			}
			return options;
		}
	}
}

 This code is a bit more efficient.  I hope this helps.

 

-Glyn

vamshi(Nani)vamshi(Nani)

HI GlynA,

 Thanks for your answer,


Please help me now on this also.

Here I am getting the value as Id, I want to pass this Id to a  method getrelateddata(), and using this value
I want fetch related list data...like account name, phone etc... 

Now how should I pass the selected value to that method.


 Thanks in advance.

GlynAGlynA

vamshi,

 

Sorry I didn't see your question until now.  I have added an action method to the controller and to the actionSupport in the VF page so that the page now displays a couple of fields from the selected account.  If you add more fields in the page, be sure to add them to the query in the displayAccount() method of the controller.  Here's the code:

 

public with sharing class SelectList
{
    public String sv { get; set; }

    public Account theAccount { get; set; }

    public List<SelectOption> value
    {
        get
        {
            List<SelectOption> options = new List<SelectOption>();
            options.add( new SelectOption( 'none', '--Select--' ) );
            for ( Account ac: [SELECT Id, Name FROM Account LIMIT 5] )
            {
                options.add( new SelectOption( ac.id, ac.name ) );
            }
            return options;
        }
    }

    public PageReference displayAccount()
    {
        theAccount = [SELECT Id, Name, Phone FROM Account WHERE Id = :sv LIMIT 1][0];
        return null;
    }
}

 

<apex:page controller="SelectList">

<apex:form >
    <apex:selectList value="{!sv}" size="1" rendered="true">
        <apex:actionSupport event="onchange" action="{!displayAccount}" rerender="output"/>
        <apex:selectOptions value="{!value}"/>
    </apex:selectList>

    <apex:pageBlock title="{!theAccount.Name}" mode="detail" id="output">
        <apex:pageBlockSection title="Account Detail" columns="2">
            <apex:outputField value="{!theAccount.Name}"/>
            <apex:outputField value="{!theAccount.Phone}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

</apex:page>

 If this helps, please click on the star to give kudos.  Thanks!

 

-Glyn