Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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');   
            alert(prodId); 
            window.parent.opener.document.getElementById(prodId).value={!oppty.Name};  
         }

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.

share|improve this question

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"  
                    action="{!saveProduct}"
                    rerender="none"
                    oncomplete="setvalue();" 
                    styleClass="buttonWidth"/>

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');   
    alert(prodId); 
    window.parent.opener.document.getElementById(prodId).value = "{!oppty.Name}";  
}
share|improve this answer
    
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. – Swapnil Kale Mar 3 '15 at 12:38
    
@SwapnilKale In example above the saveProduct() method is called as first, and then the javascript function setvalue(). – Sergey Utko Mar 3 '15 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. – Sergey Utko Mar 3 '15 at 13:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.