0

I am trying to call an apex method from my visualforce page using javascript method.

<apex:page docType="html-5.0" showHeader="false" expires="" sidebar="false" title="Connection" controller="ConnectionController">
    <apex:includeScript value="/soap/ajax/21.0/connection.js"/>
    <apex:includeScript value="/soap/ajax/21.0/apex.js"/>
    <script type="text/javascript">
    function closeWindow() {
        setTimeout(function() {
            window.close();
            CallApexMethod();
       }, 10000);
    }
    window.onload = closeWindow();
    </script>
    <apex:form id="callApexMethod">
        <apex:actionFunction name="CallApexMethod" action="{!retrieveMethod}" />
    </apex:form>
</apex:page>

And my controller is:

public class ConnectionController {
    public PageReference retrieveMethod() {
          System.debug('Inside Method');
          // some logic
          return null;
    }
}

It is not even printing System.debug in my logs. Not sure If I am missing something. I know there is a way to do it with javascript remoting as well but its a very small part and can be done by So I was trying to do that.

0

2 Answers 2

0

This is because you first close the window and then call the methods. Try to change there order

CallApexMethod();
window.close();

or it will be better if you call window.close(); from oncomplete of your action function.

2
  • nope.. actually CallApexMethod() is returning null thats why I have to put it after window.close(); Dec 19, 2017 at 10:56
  • I have alert on oncomplete but i never came. Dec 19, 2017 at 10:57
0

Addition to Tushar's Answer add reRender

<apex:form id="callApexMethod">
    <apex:actionFunction name="CallApexMethod" action="{!retrieveMethod}"   reRender="callApexMethod"/>
</apex:form>
0

You must log in to answer this question.

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