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
KavyaKavya 

Capture the selected picklist value from the visualforce page and pass it to the Apex controller!!!!

I have requirement, which is as follows..

When a user selects a particular Agent Id , all the details of that particular Agent Id should get populated on the visual force page.... I am not able to capture the selected picklist value and pass it to the Apex Controller ... Please help me..

Please guide as to where am I going wrong...

 

Class

 

public class AgentandOrderController{
Agent__c Agent;
string searchText;
List<Agent__c> result;
Integer selected;
Integer AgentId;

public String getAgent() {
        return null;
    }

 public List<Agent__c> getresult() {
      return result;
   }

public Integer getAgentId() {
return selected;
}

public void  setAgentId(Integer val) {
selected=val;
 }
 
public List<SelectOption> getItems() {
     List<SelectOption> options = new List<SelectOption>();
     options.add(new selectOption('001', '001'));
     options.add(new selectOption('002', '002'));
     options.add(new selectOption('003', '003'));
     options.add(new selectOption('004', '004'));
    
     return options;
}

public PageReference search() {
result=[select Agent_Address__c from Agent__c where Name=: ApexPages.currentPage().getParameters().get('options')];
System.debug('options : '+options);
return null;
}

}

 

 

 

Page

 

<apex:page controller="AgentandOrderController" >
 <apex:form >
<b>Agent Information</b>
  <apex:pageblock >
   <apex:pageBlockSection id="section1">
    <apex:pageBlockSectionItem >
     <apex:panelGrid columns="2">
     <apex:actionRegion >     
     <apex:outputLabel value="Agent Id" for="agentId"/>
      <apex:selectList value="{!agentId}" multiselect="false" size="1" onchange="search()" >
      <apex:selectOptions value="{!items}" />
      </apex:selectList>
     </apex:actionRegion>        
</apex:panelGrid>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>

<b>Order Information</b>
<apex:pageBlock >
<apex:pageBlockSection id="section2">
<apex:pageBlockSectionItem >
<apex:panelGrid columns="2">
</apex:panelGrid>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

 

Thank You

Navatar_DbSupNavatar_DbSup

Hi,

For this make use of apex:actionFunction.

Try the below code snippet as reference.

<apex:page controller="getpicklstval" >

<script>

function search()

{

  //  alert('hi');

    sendvaltocontroller();

}

</script>

 

 <apex:form >

 

        <apex:selectList value="{!agentId}" multiselect="false" size="1" onchange="search()" >

            <apex:selectOptions value="{!items}"/>

        </apex:selectList><br/>

      <apex:actionFunction name="sendvaltocontroller" action="{!check}"/>

    </apex:form>

   

 

 

</apex:page>

 

===Apex Controller=====

 

public class getpicklstval {

 

public String agentId{get;set;}

  

 

    public List<SelectOption> getItems() {

        List<SelectOption> options = new List<SelectOption>();

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

         options.add(new SelectOption('US','US'));

        options.add(new SelectOption('CANADA','Canada'));

        options.add(new SelectOption('MEXICO','Mexico'));

 

        return options;

    }

 

  public PageReference check()

    {

         system.debug('____________'+agentId);

        return null;

    }

 

  

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

KavyaKavya


Hi Thanks for the response, but.. here I ma trying to pass the selected picklist value to my SOQL statement in the apex controller... but I dont think i am heading in the right way.. Can i make use of <apex:param> to capture the selsected value.. please advice..

Naidu PothiniNaidu Pothini
public class AgentandOrderController
{
	public Agent__c Agent { get;set; }
	public String searchText { get;set; }
	public List<Agent__c> result { get;set; }
	public Integer selected { get;set; }
	public Id AgentId { get;set; }
	public String strAgent{ get;set; }
	
	
	public List<SelectOption> Items { get;set; }
	
	public void autoRun()
	{
		Items = new List<SelectOption>();
		
		Items.add(new selectOption('001', '001'));
		Items.add(new selectOption('002', '002'));
		Items.add(new selectOption('003', '003'));
		Items.add(new selectOption('004', '004'));
	}
	
	public PageReference search() 
	{
		result=[select Agent_Address__c from Agent__c where Name= :AgentId];  // not sure what you are trying to do.
		System.debug('options : '+options);
		return null;
	}
}
 
Page
 
<apex:page controller="AgentandOrderController" action="{!AutoRun}">
	<apex:form >
		<b>Agent Information</b>
		<apex:pageblock >
			<apex:pageBlockSection id="section1">
				<apex:pageBlockSectionItem >
					<apex:panelGrid columns="2">
						<apex:actionRegion >     
							<apex:outputLabel value="Agent Id" for="agentId"/>
							<apex:selectList value="{!agentId}" size="1" >
								<apex:actionSupport event="onchange" action="{!search}" rerender="OrderInformationBlock" />
								<apex:selectOptions value="{!items}" />
							</apex:selectList>
						</apex:actionRegion>        
					</apex:panelGrid>
				</apex:pageBlockSectionItem>
			</apex:pageBlockSection>
		</apex:pageblock>
	</apex:form>
</apex:page>

 

 

Try this. let me know if it doesnt work.

Achilles21Achilles21

Hi Kavya ,

 

I am facing the same issue  . Please let me know if you have found the solution to this !

 

Balakrishna MandulaBalakrishna Mandula

In page there is a picklist field which is having items. When we enter fields in a page we should assign field names to sObjects custom fields as follows

public class LeadController {

     public SelectOption[] leadsrcs{get;set;}

    public String title{get;set;}

     String[] leadsrc=new String[] {'----None----', 'Web','Phone Inquiry','Partner Referral','Purchased List','Other'};
       this.leadsrcs = new SelectOption[]{};
       for(String ls : leadsrc){
         this.leadsrcs.add(new SelectOption(ls,ls));
       }

    public PageReference saveandnew() {
    MyLead__c ld = new MyLead__c();
     ld.Title__c = title;  //this working
     ld.Lead_Source__c = leadsrcs; // this is not working saying like Illegal assignment from LIST<System.SelectOption> to String

 

Could you please let me know how to assign selectOption to String which is picklist?

 

 

Adil_SFDCAdil_SFDC
I am facing the same issue. let me know if any body was able to crack it. Here is my question 
https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Visualforce_Development&criteria=OPENQUESTIONS&id=906F0000000AiF4IAK
Ariel GorfinkelAriel Gorfinkel

For future references, one can use something like:
 

List<SelectOption> options = new List<SelectOption>();
List<Schema.PicklistEntry> picklistValues = Schema.getGlobalDescribe().get('My_Namespace__My_Object__c').getDescribe().fields.getMap().get('My_Namespace__My_Picklist_Field').getDescribe().getPicklistValues();
		
for(Schema.PicklistEntry picklistEntry : picklistValues) {
    options.add(new SelectOption(picklistEntry.getValue(), picklistEntry.getLabel()));
}