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
DeepikareddyDeepikareddy 

How to pass values form Actionfunction to Controller in salesforce

V.f page:
<apex:page controller="Passingvaluestocntroller">
<apex:form >

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
  
  $(document).ready(function(){
  
 $("#click").click(function(){
 
 debugger;
   alert("hello");
     var name ="Deepika";
   alert(name);
   var branch="Salesforce";
   alert(branch);
   debugger;
   var test = {"Empname":name,"Empbranch":branch};
   alert(test);
   method1();
  });
  
  });
  </script>

 
  <apex:actionFunction name="method1" action="{!method1()}"/>

<input type="button" value="test" id="click"/>
</apex:form>
</apex:page>

Controller:

 
public class Passingvaluestocntroller {


  public void method1(string name, string branch){
   system.debug('salesforce testing');
   system.debug(name);
   system.debug(branch);
  }
  
  
 
  
}

​​​​​​need to pass the javascript varible to the Apex method in salesforce.. 

Thanks D
Deepika
Best Answer chosen by Deepikareddy
Foram Rana RForam Rana R
Hi Deepika,

​​​​​​​I hope you are doing well .....!!
First of all copy below code in your Org.
I have create Visualforce page and class review it and change accroding to your requirement.

Visualforce page :
<apex:page controller="testingcontroller" >
    <script type='text/javascript'>
    function callActionMethod()
    {
        alert('@@ Test123');
        var temp ="Foram Rana";
        alert('@@ temp = '+temp);
        test(temp);
    }
    </script>
    <apex:form >
        <apex:actionFunction action="{!test}" name="test" reRender="abc">
            <apex:param assignTo="{!checkVar}" name="cht" value=""/>
        </apex:actionFunction>
        <apex:commandButton onclick="callActionMethod();" value="Click me" />
    </apex:form>
    
</apex:page>
Apex Class :
public class testingcontroller {
    
    public string message{get;set;}
    public string checkVar{get;set;}
    public string name{get;set;}
    public testwrapper testwrap{get;set;}
    
    public  testingcontroller(){
        name =  '';
        checkVar = '';
        testwrap = new testwrapper();
    }
    public void test(){
        System.debug('@@@Call 78');
        message ='Hi hello'+testwrap.name;
        System.debug('@@@ checkVar = '+checkVar);
        system.debug('testwrap.name=======>'+testwrap.name);
    }
    
    public class testwrapper{
        
        public string name{get;set;}
        public testwrapper(){
            this.name ='';
        }
    }
}
Note : Make sure in apex:actionfunction there is reRender="abc" (Write as it is)

Hope this helps you.
Let me know your review.
If this helps kindly mark it as solved so that it may help others in the future.

Thanks & Regards,
Foram Rana

All Answers

Foram Rana RForam Rana R
Hi Deepika,

​​​​​​​I hope you are doing well .....!!
First of all copy below code in your Org.
I have create Visualforce page and class review it and change accroding to your requirement.

Visualforce page :
<apex:page controller="testingcontroller" >
    <script type='text/javascript'>
    function callActionMethod()
    {
        alert('@@ Test123');
        var temp ="Foram Rana";
        alert('@@ temp = '+temp);
        test(temp);
    }
    </script>
    <apex:form >
        <apex:actionFunction action="{!test}" name="test" reRender="abc">
            <apex:param assignTo="{!checkVar}" name="cht" value=""/>
        </apex:actionFunction>
        <apex:commandButton onclick="callActionMethod();" value="Click me" />
    </apex:form>
    
</apex:page>
Apex Class :
public class testingcontroller {
    
    public string message{get;set;}
    public string checkVar{get;set;}
    public string name{get;set;}
    public testwrapper testwrap{get;set;}
    
    public  testingcontroller(){
        name =  '';
        checkVar = '';
        testwrap = new testwrapper();
    }
    public void test(){
        System.debug('@@@Call 78');
        message ='Hi hello'+testwrap.name;
        System.debug('@@@ checkVar = '+checkVar);
        system.debug('testwrap.name=======>'+testwrap.name);
    }
    
    public class testwrapper{
        
        public string name{get;set;}
        public testwrapper(){
            this.name ='';
        }
    }
}
Note : Make sure in apex:actionfunction there is reRender="abc" (Write as it is)

Hope this helps you.
Let me know your review.
If this helps kindly mark it as solved so that it may help others in the future.

Thanks & Regards,
Foram Rana
This was selected as the best answer
DeepikareddyDeepikareddy
Thank you.. it solved.. @Foram Rana