0

I'm trying call this:

<apex:actionFunction name="doCallout" action="{!callout}" />

From javascript like this:

<script>
window.onload=function()
{
    doCallout();
}
</script>

It works, but it seems to be stuck in either an infinite loop OR its constantly rerendering the page every half second. Adding "rerender" to the actionFunction doesn't seem to have any effect no matter what value I use.

Currently I'm using a command button to call the function(which is a callout to an outside webservice using REST) like this:

<apex:commandButton value="Callout" action="{!callout}" rendered="{!$CurrentPage.parameters.offline!='1'}"/>

The commandButton works fine and doesn't get the page to refresh every half second, but I need this callout to happen upon the page loading for seemless RESTful API callout experience.

Any thoughts?

EDIT: Pasting Full page code:

<apex:page id="page" controller="RSAPI_Controller" >
<apex:form id="form" >
    <apex:actionFunction name="doCallout" action="{!callout}"  />
    <apex:sectionHeader title="RESTFUL Callout Viewer" />
    <apex:pageMessages id="pageMessages" />
    <apex:pageBlock >
        <apex:pageBlockButtons >


            <apex:commandButton value="Callout" action="{!callout}" rendered="{!$CurrentPage.parameters.offline!='1'}"/>
            <apex:commandButton value="Deserialize" action="{!deserializeQuestions}" rendered="{!$CurrentPage.parameters.offline!='1'}"/>

            <apex:commandButton value="Callout (offline)" action="{!callout}" rendered="{!$CurrentPage.parameters.offline=='1'}" />
            <apex:commandButton value="Deserialize (offline)" action="{!deserializeQuestions}" rendered="{!$CurrentPage.parameters.offline=='1'}" />



        </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1" title="HttpRequest" collapsible="false" >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="setMethod()" />
                <apex:selectList size="1" value="{!requestMethod}" >
                    <apex:selectOptions value="{!MethodOptions}" />
                </apex:selectList>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="setEndpoint()" />
                <apex:inputText value="{!requestEndpoint}" style="width:90%" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>

        <apex:pageBlockSection columns="1" title="HttpResponse" collapsible="false" >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="getStatusCode()" />
                <apex:outputText value="{!responseStatusCode}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="getStatus()" />
                <apex:outputText value="{!responseStatus}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="getBody()" />
                {!responseBody}
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>


    </apex:pageBlock>
</apex:form>
                <script>
            window.onload=function()
            {
                doCallout();
            }
            </script>

  • can you try with jquery .ready function ? – Himanshu Oct 28 '15 at 17:39
  • indeed i have with this: <script> jQuery(document).ready(function($) { } </script> Same Results – Rick Walker Oct 28 '15 at 17:55
  • ok, can yo post your full code. you might be missing something else ? – Himanshu Oct 28 '15 at 18:02
3

Try this structure and make sure that tag <script> is not part of apex form:

  <apex:form>
     <!-- other stuff -->
     <apex:actionFunction name="doCallout" action="{!callout}" rerender="none"/>

    <!-- other stuff -->    
  </apex:form>
    <script>
    window.onload=function()
    {
        doCallout();
    }
    </script>

Note: I am assuming that you don't want to use action attribute of <apex:page>

  • 2
    Also, you shouldn't use window.onload, but instead window.addEventListener('DOMContentReady', function...). Otherwise, future changes to the platform may result in the code breaking salesforce (or the other way around, depending on which code runs first). – sfdcfox Oct 28 '15 at 18:11
  • Thank you for the response. I'm not using "action' because it results in a System.NullPointerException. I will edit my post with the full Page code. – Rick Walker Oct 29 '15 at 19:36
0

It will definitely result in infinite loop. It happens in this way:- 1. Your page loads and on load it calls actionfunction "doCallout" 2. On execution "doCallout" refreshes (i.e. reloads) the page 3. Page loads again and step 1 is repeated.

Hence, this will always result in infinite loop. The essential thing is, either you make your call to "doCallout" asynchronous i.e. use "rerender"

OR, you modify javascript to detect if page is already refreshed.

If you are just trying to call a controller method on page load, I would recommend using "action" attribute of <apex:page>.

  • This sounds correct. I'm just to new to understand where its breaking. I will post my full Page code. If i can post other files i will as well. – Rick Walker Oct 29 '15 at 19:36
  • If using <apex:page> action method generates NullPointerException, then you should look at that first. You can refer to debug logs (help.salesforce.com/…) and fix the issue. "Action" method is preferred way to invoke actions on page load. – Anshul Oct 29 '15 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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