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
Adi85Adi85 

not able to get values into the controller from the VF page

Hi all,

 

am new to visual force and am trying to get values from my visual force page for further processing but am getting null values in my controller .

 

Can any one let me know what is the problem??

 

Here is my code snippet.

 

VF page :

------------

 

 

<apex:page controller="paginationController">
<apex:form >
    <apex:pageBlock mode="edit" title="Search">
    <script>
    function scriptSearch(){
        //alert(document.getElementById("firstName").value);
        //alert(document.getElementById("lastName").value);
        //alert(document.getElementById("country").options[document.getElementById("country").selectedIndex].value);
        searchCall(document.getElementById("firstName").value,document.getElementById("lastName").value,document.getElementById("country").options[document.getElementById("country").selectedIndex].value);
    }
    </script>
    <apex:actionFunction name="searchCall" action="{!searchFunctionality}">
    <apex:param name="firstname" value="" />
    <apex:param name="lastname" value="" />
    <apex:param name="country" value="" />
    </apex:actionFunction>
        <table>
            <tr>
                <td>First Name : <input type="text" id="firstName"/></td>
                <td>Last Name: <input type="text" id="lastName"/></td>
                <td>
                Country : <select id="country">
                <option value=""></option>
                <apex:repeat value="{!countries}" var="country">
                <option value="{!country}">{!country}</option>
                </apex:repeat>
                </select>
                </td>
                <td><apex:commandButton onclick="scriptSearch();" value="Search"/></td>
            </tr>
       </table>
    </apex:pageBlock>
    
    <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!queryString}" />           
  </apex:pageBlock> 
  
</apex:form>
</apex:page>

 

 

Controler :

------------

 

 

public class paginationController {

 

public string queryString{get;set;}

 

public List<SampleObject001__c> userDetails{get;set;}

 

public paginationController(){

 queryString='Select First_Name__c,Last_Name__c,Country__c From SampleObject001__c';

 executeQuery();

}

public List<String> countries {

    get {

      if (countries == null) {

 

        countries = new List<String>();

        Schema.DescribeFieldResult field = SampleObject001__c.Country__c.getDescribe();

 

        for (Schema.PicklistEntry f : field.getPicklistValues())

          countries.add(f.getLabel());

 

      }

      return countries;          

    }

    set;

  }

 

 public PageReference searchFunctionality(){

 

  String firstName = Apexpages.currentPage().getParameters().get('firstname');

    String lastName = Apexpages.currentPage().getParameters().get('lastname');

    String country = Apexpages.currentPage().getParameters().get('country');

 

    system.debug('-----firstName-------'+firstName);

    system.debug('-----lastName-------'+lastName);

    system.debug('-----country-------'+country);

 

    if(firstName!=null && !firstName.equals('')){

     queryString+='and First_Name__c LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';

    }

    if(lastName!=null && !lastName.equals('')){

     queryString+='and Last_Name__c LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';

    }

    if(country!=null && !country.equals('')){

     queryString += ' and Country__c includes (\''+country+'\')';

    }

 

    executeQuery();

  return null;

 }

 

 public void executeQuery(){

  try{

  system.debug('-----QUERY-------'+queryString);

  userDetails= Database.query(queryString);

  }catch(Exception e){

 

  }

 }

}

 

 

 

please help me to resolve this issue.

 

MarkWaddleMarkWaddle

There are two problems that I can see.

 

Your button is making an ajax call, however it is not canceling the default behavior of the commandButton which is to submit the form.

 

Fix this with

 

<apex:commandButton onclick="scriptSearch();return false;" value="Search"/>

 

 

Also, your actionFunction needs to be configured to refresh your debug pageBlock after the ajax call is complete. Check out the reference for apex:actionFunction.

 

<apex:actionFunction name="searchCall" action="{!searchFunctionality}" rerender="debug">

 

 

You might also consider using the apex:actionStatus to show the status of the ajax call.


Regards,

Mark

Adi85Adi85

Thanks allot mark for giving me the clear information. Now i can able to get the values into my controller.

 

Thanks allot for your help. 

 

regards,

Adi.

MarkWaddleMarkWaddle

Would you mind marking my post as the solution?

 

Thanks,

Mark