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
Vishal_ThoriyaVishal_Thoriya 

how to pass values of javascript variable to controller?

hey Ragjesh your code helped me a lot but am getting null value in my controller can you suggest me the exact solution of this problem

 

 

here is my code:

Apex Code:

<apex:actionFunction name="sendTimeStamp" action="{!sendTimeStamp}">
    <apex:param name="x" value="x" assignTo="{!timeStampValue}" />
    </apex:actionFunction>

javascript code:

timeStamp = Number(new Date());
          sendTimeStamp(timeStamp);

controller code:


public string sendTimeStamp() 
    {
        return timeStampValue;
    }
    public string timeStampValue{
    get
    {
        timeStampValue = timeStamp;
        return timeStampValue;
    }
    set;}

 please Help me i am newbie in salesforce

 

i dont know where i am making mistakes?

Best Answer chosen by Admin (Salesforce Developers) 
Devendra NataniDevendra Natani

Hello,

 

Please use the following code. In the rerender property of actionFunction please specify the id of section which you want to rerender. 

 

VF Page - Action function
<apex:actionFunction name="sendTimeStamp" action="{!sendTimeStamp}" rerender="pb"> <apex:param name="x" value="" assignTo="{!timeStampValue}" /> </apex:actionFunction> javascript code: timeStamp = Number(new Date()); sendTimeStamp(timeStamp); controller code: public class TestController{
public string timestampValue{get;set;}

public TestController(){
timeStampValue = '';
}
public void sendTimestamp(){
system.debug('timestampValue' + timeStampValue);
}
}

 

All Answers

IspitaIspita

If you check the forum you will find many readymade code for this purpose:-

 

I found this 1 just check  and compare what are u doing different:-

 VF Coded:-

<apex:page controller="OurTest">

<apex:form>

<apex:actionFunction name="ShuffleByID" action="{!ShuffleByRecordID}" rerender="out"/>  

                    <apex:outputPanel id="out">

                                         <apex:pageBlock >                                                                                                                                                                                                                  

                <apex:pageBlockTable value="{!studentListLongArr}" var="enrollment" rendered="{!EnrollmentListSize > 0}">          

                     <apex:column >

                       <apex:pageBlockTable value="{!enrollment.Registered_Student__r}" var="stulist">

                      

                          <apex:column >

                                         <apex:facet name="header">

                                                             <apex:commandLink onclick="ShuffleByID();return false;" action="{!doSort}" rerender="out">Record ID

                                                                                 <apex:param name="sortField" value="{!stulist.Name}" assignTo="{!YourName}"/>

                                                             </apex:commandLink>

                                         </apex:facet>

                                         <apex:outputLink value="{!stulist.id}" target="_top">{!stulist.Name}</apex:outputLink>

                          </apex:column>

</apex:form>                                                                                                                   

</apex:page>                                                                                             

 

Controller Code:-

 

public class OurTest()

{

                    public string YourName {get;set;}

                                         public OurTest()

                    {

                    system.debug('++++++' +YourName);

                    //now you will get the value foYourName which you are passing from VF param tag.

                    }

                    // Rest of the code can be put here

 

}

Ispita_NavatarIspita_Navatar

 Hi,

           You can pass the variable to controller by using the below code snippet.

            Below is the sample code to work around on this.

 

           <apex:inputhidden value="{!Propval}" id="hdnobj"/>

                <script>

                                var crtUrl = "{!$CurrentPage.URL}";

                                document.getElementById('pageid:formid:hdnobj').value = crtUrl;

           </script>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

Vishal_ThoriyaVishal_Thoriya

is there any way to pass the javascript variable values to my controller without using concept opf url.......

 

please suggest me a solution based on my requirement.........

Devendra NataniDevendra Natani

Hello,

 

Please use the following code. In the rerender property of actionFunction please specify the id of section which you want to rerender. 

 

VF Page - Action function
<apex:actionFunction name="sendTimeStamp" action="{!sendTimeStamp}" rerender="pb"> <apex:param name="x" value="" assignTo="{!timeStampValue}" /> </apex:actionFunction> javascript code: timeStamp = Number(new Date()); sendTimeStamp(timeStamp); controller code: public class TestController{
public string timestampValue{get;set;}

public TestController(){
timeStampValue = '';
}
public void sendTimestamp(){
system.debug('timestampValue' + timeStampValue);
}
}

 

This was selected as the best answer