On click of apex: command button I have to call method in controller so that I can get values. After calling the apex i have to call an javscript function.

I have used action function to call apex function.

<apex:commandButton value="Save"  onclick="saveValuesDetails();" oncomplete="setvalue();" styleClass="buttonWidth"/> &nbsp; 
 <apex:actionfunction action="{!saveProduct}" name="saveValuesDetails" />  

and in javascript I am calling as

 function setvalue()
            var prodId = getParameterByName('product');   

in controller I am having an function

Public saveValuesDetails(){
//here I am initiallizing values.

I have to first inititalize my values and the assign it thorough javascript. Please help.

1 Answer 1


First of all if you don't use rerender parameter on the command button the page will reload. So your oncomplete action get lost. To prevent the page reload you need to use rerender="none" attribute or if you call your apex method through the actionFunction use return false; command after that call.

<apex:commandButton value="Save"  

In apex class your method should have the same name like in the actionFunction action attribute:

public void saveProduct(){
    //here I am initiallizing values.

In the javascript function while assigning a string value to the DOM element you should use quotes:

function setvalue(){  
    var prodId = getParameterByName('product');   
    window.parent.opener.document.getElementById(prodId).value = "{!oppty.Name}";  
  • Thanks for replay On click of command button I am getting value in debug log. But in script I am getting null value. I think this is because script is getting called first and then my apex method is getting invoked. is their any solution to invoke apex method first and then call javascript method. Or is their any way to call java script function in from controller. Mar 3, 2015 at 12:38
  • @SwapnilKale In example above the saveProduct() method is called as first, and then the javascript function setvalue(). Mar 3, 2015 at 12:47
  • @SwapnilKale Ahh just added rerender="none" and a normal action directly to the commandButton and removed the actionFunction as unnecessarily (to prevent the page reload and to get the oncomplete working). Test it, it should work. Mar 3, 2015 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .