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 

Passing a value to a variable from visualforce page to controller


I have a String variable in my controller call ChildQuery. I want to pass a value to the string variable from Visualforce page without any javascript or button. How can that be possible? This value needs to be hidden and auto assign as soon as the visualforce page is opened.  

//set the value of Childquery to "write some string value here like ABCD"  without calling <script></script>
// maybe using these tags below? 

<!-- apex:inputHidden  / -->
<!-- apex:param  / -->
<!-- apex:variable / -->

This is what my class would look like. I'm writing a generic class which would be used by many many different visualforce page. So cannot set the value of the variavle in the controller. It has to pass through the visualforce page so I can throw any page and just call this controller class. 
Class sObject {

public string ChildQuery {get; set;}

    public sObjectController(ApexPages.StandardController controller){
        system.debug('THIS IS THE VALUE I'M GETTING:' + ChildQuery);



Briana L.Briana L.
public sObjectController(ApexPages.StandardController controller)
ChildQuery = Apexpages.currentPage().getparameters().get('ChildQuery');        
system.debug('THIS IS THE VALUE I'M GETTING:' + ChildQuery);
<apex:param name="ChildQuery" value="Whatever you want it to be" assignTo="{!ChildQuery}/>

I have tried that. It is returning Null Value. Does Param has be invoked by something?
Maharajan CMaharajan C
 Hi Semira,

Class sObject
public string ChildQuery {get; set;}
public sObjectController(ApexPages.StandardController controller)
{ system.debug('THIS IS THE VALUE I'M GETTING:' + ChildQuery);
} }

<apex:inputText value="{!ChildQuery}" label="Input"/> 


<apex:inputHidden value="{!ChildQuery}" id="theHiddenInput"/>

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Waqar Hussain SFWaqar Hussain SF
You will have to use apex:actionfunction to send value from vf page to apex controller.

See example
InputText and InputHidden does not work. 

If I have to use apex:actionfunction, I have to write <script></script> which I was trying to avoid. Is there really no other way? I was trying to pass a string of query like "select id from [specific object] where [specific field] =: RecordID" 

I'm trying to use this string in my controller where I can say 

String childQuery = //get the string value from VS page. 
Briana L.Briana L.
Try modifying your visualforce page like this:
<apex:attribute name="ObjName" value="Object__c" assignTo={!ObjName}/>
<apex:attribute name="FieldNm" value="Field__c" assignTo="{!FieldNm}/>
<apex:attribute name="RecordID" type="Id" value="Field__c" assignTo="{!FieldNm}/>

And controller like this:
public string ObjName{get; set;}
public string FieldNm{get; set;}

public sObjectController(ApexPages.StandardController controller){
ObjName = Apexpages.currentPage().getparameters().get('ObjName');
FieldNm = Apexpages.currentPage().getparameters().get('FieldNm');
RecordId = Apexpages.currentPage().getparameters().get('id');
String childQuery = 'SELECT id from ' + ObjName + ' where ' + FieldNm + '=: " + RecordID;        
system.debug('THIS IS THE VALUE I'M GETTING:' + ChildQuery);

That defeats the purpose of my controller. As I have mentioned before, I wrote a generic Controller Sobject which does not have any specific object or fields defined in it. Because I want to use any visualforce page from any object and be able to call the controller without touching the controller class ever becuase it will read everything from the visualforce page. 

This is why I need to pass the query string from the visualforce page. Then in my controller, it will read as string and call Databate.query(query)
Waqar Hussain SFWaqar Hussain SF
Doesn't seems possible without using action function. You must have to use action function of Javascript funtion to send data from VF page to apex controller.
So far I have this.. 
                    function(value) { 
				<!-- apex:actionFunction action="{!setParamsmethod}" name="setParams">
               		<apex:param name="ChildRecordsQuery" value="SELECT Id, Name,Shipped__c, Returned__c FROM Material_Requisition_line_item__c where Material_Requisition__c =: RecordId  order by CreatedDate ASC" assignTo="{!ChildRecordsQuery}"/>
                </apex:actionFunction -->                    

My controller: 
public void setParamsmethod(){
        ChildRecordsQuery = ApexPages.currentPage().getParameters().get('ChildRecordsQuery');

I'm not familiar with actionfunction with javascript. Is this right? It's still not returning anything.  
Waqar Hussain SFWaqar Hussain SF
public class VFTEST {
    public string QueryString { get; set; }

    public VFTEST() {

    public void getQueryString() {
        system.debug('::' + queryString);

VF page
<apex:page controller="VFTEST">
<apex:form >

window.onload = function() {
    var param = 'SELECT Id, Name,Shipped__c, Returned__c FROM Material_Requisition_line_item__c where Material_Requisition__c =: RecordId  order by CreatedDate ASC';

<apex:actionFunction action="{!getQueryString}" name="setParam" reRender="none">
    <apex:param name="QueryString" value="" assignTo="{!QueryString}"/>

<apex:inputtext value="{!QueryString}" />
<!-- <input type="text" value="{!QueryString}" /> -->

Briana L.Briana L.

With the code I provided, you would declare the Object name and Field name as string variables inside the visualforce page. This makes the controller dynamic for different visualforce pages, as long as you are passing difference object and field name strings in.
Prakhar Saxena 19Prakhar Saxena 19

I used a Visualforce component in between to pass the variable from a Visualforce Page to the Apex Controller. The Controller remains the same in this case and can be used for other Visualforce Pages as well. (Just call the Visualforce Component from the page passing different values to the attribute.)
<apex:page lightningStylesheets="true">
    <c:CustomLinksComponent linkType="New"/>   
<apex:component controller="HomePageLinksCtrl">
  <apex:attribute name="linkType" type="String" assignTo="{!linkTypeVar}" 
   description="Type of Link"/>
public class HomePageLinksCtrl {    
    String linkTypeVar;    
    public void setLinkTypeVar(String link){
        linkTypeVar = link;
        System.debug('Value is: '+linkTypeVar); // Output: Value is: New
    public String getLinkTypeVar(){
        return linkTypeVar;

Whatever value is passed from the Visualforce page in the Visualforce Component CustomLinksComponent, it gets assigned to the variable linkTypeVar.

This value can be used in the variable's setter method of the controller. Additionally, this value can be used to make further SOQL queries.