I'm trying to pass a JavaScript Variable to my Controller and do a query with the value.
Here's what I got (only necessary code):
My Controller:
public with sharing class MyClass{
public String AccountList { get; set; }
public String myString {get; set;}
// Method for testing a hidden field functionality
public PageReference myMethod(){
System.debug('myString: ' + myString);
return null;
}
...
public static String getlstAccount() {
List < Accountwrap > lstwrap = new List < Accountwrap > ();
String Abfrage='SELECT Id, Name, Phone, BillingCity FROM Account WHERE Id=\''+MyString+'\'';
List < account > lstacc = database.query(Abfrage);
...}
My VF Page:
<script>
function setVar(param){
jQuery('[id$=myHiddenField]').val(param);
passStringToController();
var List = {!lstacc};
}
</script>
<apex:form >
<!-- Hidden field to store a new value of the variable -->
<apex:inputHidden value="{!myString}" id="myHiddenField"/>
<!-- Action function for the rerendering -->
<apex:actionFunction name="passStringToController" action="{!myMethod}" rerender="myHiddenField"/>
<!-- A command button for sending a call to the function -->
<apex:commandButton value="Test me" onclick="setVar('new value'); return false;" />
</apex:form>
So I want to set the variable and change the AccountList (dependent on my variable) after the page is loaded.
I got the code for setting the variable in controller from another question, but it seems it doesn't work, and it's always null or empty. I think I'm missing something important.
Thanks in advance!