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:

function setVar(param){

var List = {!lstacc};

<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;" />

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!

  • Are you getting any error in browser console?
    – Saroj Bera
    Oct 22 '15 at 16:40
  • No, no error in browser console. I'm very new to SF. I think I have to check the controller logs somehow and update when I found sth which is maybe helpful.
    – AlexH.
    Oct 23 '15 at 8:38
  • @SarojBera I looked in logs of controller and it said: VARIABLE_ASSIGNMENT [EXTERNAL]|this|{"myString":"new value". So I guess setting the variable works fine. But my page is loaded when myString is null/empty. Is there any way to get the actual value of my variable after I set it without refreshing the page? And even if I refresh, the Controller refreshs too and the variable is null again? Thanks!
    – AlexH.
    Oct 23 '15 at 14:54
  • Okay, I got my mistake... I need to rerender my {!lstacc}. I wasnt confirm with rerendering. Like I said, I'm very new, sorry :). The only thing I want now is to avoid Buttons, but I guess there is a way to rerender from functions? Thanks anyway mates.
    – AlexH.
    Oct 23 '15 at 15:14

Use jQuery('[id$="myHiddenField"]').val(param);

You seem to have missed the double quotes.

  • this is not an issue. Alex has already mentioned he has taken this from another example. Please check this link sravalaparthi.blogspot.in/2014/08/…
    – Saroj Bera
    Oct 22 '15 at 16:44
  • Yes, setting the variable works fine I guess, even without the double quotes.
    – AlexH.
    Oct 23 '15 at 14:56
  • ok...i think AlexH has got the solution...it can be marked closed :)
    – ipradhansk
    Oct 24 '15 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.