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
Achilles21Achilles21 

Pass Values from VisualForce Page to Custom Controller

Hi ,

 

I am new to VF and Apex Development and creating a sample application .

My requirements are as below .

 

- have to create a simple flight booking application where the user will select Source and Destination from the picklists on the VF page .

 

- Based on the user's selection , a SOQL needs to run on Apex Controller and return results on VF page that match the criteria.

 

- I am trying getter method but It is accepting only Custom Object data type . getting various errors .

 

Question :

 

Can I be enlighten on how picklist values be sent to controller that can be used as a string , which later can be used in a where clause !!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Achilles21Achilles21

Hi,

 

Thanks a lot for the reply , soon as I posted this query i got a solution for it as well .

 

Below is the code

 

------------VF-----------------

 

 

<apex:page controller="myControl">
    <apex:form >
        <apex:selectList value="{!main}" multiselect="false" size="1">
            <apex:selectOptions value="{!names}"/>
        </apex:selectList><p/>

        <apex:commandButton value="Test" action="{!test}" rerender="out" />
    </apex:form>

    <apex:outputPanel id="out">
      <apex:dataTable value="{!mains}" var="m" cellPadding="4" border="1">
              <apex:column >
               <apex:facet name="header">Flight No.</apex:facet>
                {!m.Flight_No__c}
              </apex:column>
              n> 
                </apex:dataTable>
                    </apex:outputPanel>
</apex:page>

 

 

 

 

-----------------Controller--------------------

 

public class myControl{

              
        
        String src;
        List<Flight_db__C> result;
  
        public PageReference test() {
                    result  = (List<Flight_db__c>)[select f.Flight_No__c from Flight_db__c f where f.leaving_from__c = :src];

                       return null;
        }
           
        public List<SelectOption> getNames() {
            List<SelectOption> options = new List<SelectOption>();
            for(Flight_db__C f : [SELECT leaving_from__c from Flight_db__C])
            options.add(new SelectOption(f.Leaving_from__C,f.leaving_from__C));
            return options;
      
          }
             public String getMain(){
             return src;
             }
          
             public List<Flight_db__c> getMains(){
             return result;
             }
 
 

            public String setMain(String src){
              this.src=src ;             
      
         
  return null;
                   
         
        
      }
           }

 

All Answers

Siddharth Birari.ax1164Siddharth Birari.ax1164

Hi,

 

it would have been better if you would have provided the code that you have written.

 

Although, following is what i can suggest from my understanding for your problem.

 

Your apex code would be something like this

 

List<city__c> lstCities = new List<city__c>();

String selectedCity {get; set;}

.

.

.

Provide the values to the list through constructor

 

//getter

public List<city__c> getlstCities(){

 

  return this.lstCities;

}

 

 

Here goes your VF

      <apex:selectList value="{!selectedCity">

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

      </apex:selectList>

 

 

I guess this will give you an idea..

 

Achilles21Achilles21

Hi,

 

Thanks a lot for the reply , soon as I posted this query i got a solution for it as well .

 

Below is the code

 

------------VF-----------------

 

 

<apex:page controller="myControl">
    <apex:form >
        <apex:selectList value="{!main}" multiselect="false" size="1">
            <apex:selectOptions value="{!names}"/>
        </apex:selectList><p/>

        <apex:commandButton value="Test" action="{!test}" rerender="out" />
    </apex:form>

    <apex:outputPanel id="out">
      <apex:dataTable value="{!mains}" var="m" cellPadding="4" border="1">
              <apex:column >
               <apex:facet name="header">Flight No.</apex:facet>
                {!m.Flight_No__c}
              </apex:column>
              n> 
                </apex:dataTable>
                    </apex:outputPanel>
</apex:page>

 

 

 

 

-----------------Controller--------------------

 

public class myControl{

              
        
        String src;
        List<Flight_db__C> result;
  
        public PageReference test() {
                    result  = (List<Flight_db__c>)[select f.Flight_No__c from Flight_db__c f where f.leaving_from__c = :src];

                       return null;
        }
           
        public List<SelectOption> getNames() {
            List<SelectOption> options = new List<SelectOption>();
            for(Flight_db__C f : [SELECT leaving_from__c from Flight_db__C])
            options.add(new SelectOption(f.Leaving_from__C,f.leaving_from__C));
            return options;
      
          }
             public String getMain(){
             return src;
             }
          
             public List<Flight_db__c> getMains(){
             return result;
             }
 
 

            public String setMain(String src){
              this.src=src ;             
      
         
  return null;
                   
         
        
      }
           }

 

This was selected as the best answer