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
Rajesh ShahRajesh Shah 

Passing values from Javascript to Controller

Is there any to pass to parameters from a javascript to a actionFunction?

For eg  I have a var 'x' in javascript set to some value. On some condition I need to call a actionFunction which calls a controller. I need to pass the value of x to the controller. I can pass using param tag of actionFunction to controller but how to pass x to the actionFunctio?

Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

Using actionFunction is like any normal javascript function - just pass in parameters like you would normally.

 

<apex:actionFunction name="doStuff" action="{!doStuff}"> <apex:param name="x" value="x" assignTo="{!somevariable}" /> </apex:actionFunction> <script> function dosomejavascript() { // set up my param to pass to action function var myParam = 'abc'; /* call my action function - myParam variable should be set to 'somevariable' in my controller */ doStuff(myParam); } </script>

 

All Answers

XactiumBenXactiumBen

Using actionFunction is like any normal javascript function - just pass in parameters like you would normally.

 

<apex:actionFunction name="doStuff" action="{!doStuff}"> <apex:param name="x" value="x" assignTo="{!somevariable}" /> </apex:actionFunction> <script> function dosomejavascript() { // set up my param to pass to action function var myParam = 'abc'; /* call my action function - myParam variable should be set to 'somevariable' in my controller */ doStuff(myParam); } </script>

 

This was selected as the best answer
mat_tone_84mat_tone_84

I have similar problem but i don't solve, this is my easy code:

 

 

<apex:page standardController="opportunity" >
date {!(Opportunity.lastmodifieddate)}

<script src="/soap/ajax/10.0/connection.js" type="text/javascript">
</script>
<script type="text/javascript" src="/js/functions.js"></script>
<script>
[... some my code...]
var newvar1 = '1'
</script>
xxxxxxxxx
</apex:page>

 

 Into the "xxxxxxxxx" I have to substitute a code which auto-save the value of "newvar1" into a field of opportunity when I open page of every opportunity

How can I do it ?

thanks

 

Vishal_ThoriyaVishal_Thoriya

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

 

Joe SantosJoe Santos

Thanks so much. I was looking all over for a clean, easy-to-understand example of how to do this. You saved the day.

dipsdips

Hi all,

 

Can anyone tell me the exact way to pass a variable from js to controller, please?

 

javascript code:

 var csvVals = "abc";

callToSetSelectedFields(csvVals);

 

vf code:

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

 

controller:

public String RHSValuesForFields
    {
        get
        {
            RHSValuesForFields = csvVals;
            return RHSValuesForFields;
        }
        set;
    }
    
    public String callToSetSelectedFields()
    {
       //RHSValuesForFields = csvFieldVals;
       return RHSValuesForFields;
    }

 

This code is giving error for unknown csvFieldVals

 

How should i use the csvFieldVals in controller?

Joe SantosJoe Santos

In your controller, set up the property the parameter binds to like this:

 

public String RHSValuesForFields { get; set; }

 

callToSetSelectedFields should be able to get the value out of that.

 

dipsdips

Thanks Joe.

 

But after doing that i get null value in the function.

 

The work around i used was adding a hidden field and setting its value in javascript then using that fields value in controller.

lakslaks

Hi Dips,

 

Can you please post your code snippet if possible. (with the work around)

I am facing a similar problem. It is a bit urgent.

 

Thank you in advance.

 

 

Regards,

Lakshmi.

dipsdips

laks,

 

The page contains <apex:inputHidden id="hiddenSecId" value="{!RHSValsForFields}"/>

 

Then i set the field value from javascript as:

 

var hiddenSec = document.getElementById(hiddenSecId);
document.getElementById(hiddenSecId).value = totalValsSelected;

 

This way the 'RHSValsForFields' field value is set from javascript and can be used in controller now.

 

Hope this solves your problem.

 

lakslaks

Hi Dips,

 

Thank you for the reply. 

 

I figured it out and did it with a combination of hidden fields, action function and javascript.

 

 

Regards,

Lakshmi.

pradeepkumar danipradeepkumar dani
ReRender="" is required otherwise values are showing as null in my case. 
Shohrat MuhamovShohrat Muhamov
Can anyone share snipped of the working code for this post please?

Thank you,
Shohrat