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 javascript values to the controller

Hello guys,


I need your help.


I am using some API to get the exchange rates using javascript and I am successful in that.


Now, I want these rates should be passed to the Save method defined in the controller.


To clarify, my JS function is :

function newnee(){
		dollar1 = fx.convert(1, {from: 'USD', to: 'EUR'});

 As you can see, I have set the value of dollar1. Here is my save in VF :

<apex:commandButton action="{!NewSave}" value="Save" onclick="newnee();"/>

 Here is the Save in my controller :

 public Pagereference NewSave()
         //.......Some code and then insert ......//
            insert objInv ;
            PageReference acctPage = new ApexPages.StandardController(objInv).view();
            return acctPage;


I have tried Actionfunction as well as inputhidden. But seems I am missing something.


What would be the best way to pass the values to the Save function(Custom)?


Can someone help me?




Rahul SharmaRahul Sharma

There are 2 options:


1. Use javascript remoting, call JS function(remote action call is made within it) from commandbutton onclick event. Dont use call controller action as it would be taken care with remoting.


2. apex:actionFunction, From button's onclick event call js function to perform calculation of exchange rate. Then pass the value to apex:actionfunction and use apex:param for pasing parameter to controller method.


apex:actionFunction is simpler to use, but remoting is much faster.


Shailesh PatilShailesh Patil

Thanks a lot Rahul.


But, let me explain my scenario a little more.


I have to pass the variable value onclick of save. How do I couple apex:actionfunction in this case?


When I tried using action function with a different function other than Save, the page was executed TWO times. One - The actionfunction got executed and Two - The save executed (There were two debug instances).


Can you please correct my code for javascript remoting as well as for actionfunction? I will really appreciate this. I have truncated lot of code it between for your readability.


Controller  : 


public with sharing class Invoice_Helper
   Public Transient String attName = '';
   Public Transient String testingB = 'Test';
   Public Transient String attOwnerId = '';
   Public Transient String attParId = '';
   public String theValue{get;set;}
     public Invoice_Helper()
       objInv = new  Invoice__c();  
      Public Transient String strCategory{get;set;}
      Public Transient String strPay{get;set;}
      Public Transient String strStatus{get;set;}
      Public Transient String strInv{get;set;}
      public Transient Id strId{get;set;}
      Public Transient String strInvLast{get;set;}
      public Map<String,Id> lstCat = new Map<String,Id>();
      public Map<String,Id> lstPay = new Map<String,Id>();
      public Map<String,Id> lstStat = new Map<String,Id>();
    public String valueOne { get; set; }
   // public String valueTwo { get; set; }
   // public PageReference iWantMyJSValues() {
       // valueOne = Apexpages.currentPage().getParameters().get('one');
       // valueTwo = Apexpages.currentPage().getParameters().get('two');
       // system.debug('===========iWantMyJSValues=============='+valueOne+'iWantMyJSValues     '+valueTwo);
     //   return null;
   // }
      public Pagereference NewSave() // This is the custom save method
            objInv.Payment__c = lstPay.get(strPay);
            objInv.Category__c = lstCat.get(strCategory);
            objInv.Status__c = lstStat.get(strStatus);
            objInv.Invoice_Date__c = objInv.Invoice_Date__c;
            insert objInv ;
            PageReference acctPage = new ApexPages.StandardController(objInv).view();
            return acctPage;
           public Pagereference saveNnew()
                objInv.Payment__c = lstPay.get(strPay);
                objInv.Category__c = lstCat.get(strCategory);
                objInv.Status__c = lstStat.get(strStatus);
                insert objInv ;
                PageReference pageRef = new PageReference('/apex/Invoice_Override');
                return pageRef;
            public Pagereference cancelOn()
                PageReference pageRef = new PageReference('/a01/o');
                return pageRef;



Page :


<apex:page Controller="Invoice_Helper" tabStyle="Test_Object__c">
<script src="{!$Resource.jquery}"></script>
<script src="{!$Resource.MoneyJS}"></script>
<Script type ="text/javascript">
		var dollar1=0;
		var GBP1=0;
		/*Some code to set the values here*/
		function newnee(){
		document.getElementById('{!$Component.hdnField}').value = dollar1;


 <apex:sectionHeader title="Invoice Edit" subtitle="New Invoice" />
     <apex:form >
                <apex:inputHidden id="hdnField" value="{!valueOne}" />
      <apex:pageBlock title="Invoice Edit" id="block">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!NewSave}" value="Save" onclick="newnee();"/>
                <apex:commandButton action="{!saveNnew}" value="Save and New"/>
                <apex:commandButton action="{!cancelOn}" value="Cancel" immediate="true" />