0

I have a VF page render as PDF, now I want to call two methods the second one "generateImageDoc" must start when the first "loadData" is finished, and both must run during page load. I'm trying to do that whit this code:

<apex:page controller="CrashDossierPDFController" renderAs="pdf" applyBodyTag="false" applyHtmlTag="false" showHeader="false">
    <apex:variable var="customerLogo" value="{!customerLogo}"/>
    <apex:form>
        <apex:actionFunction name="loadData" action="{!loadData}"  status="myStatus" oncomplete="generateImageDoc();"/>
    </apex:form>
    <script>
        // start the process that create the crash images
        window.onload = onload;
        function onload{
            loadData();
        }
    </script> 

But the methods seem not starting at all. How can I achieve that?

4
  • You should add rerender in your actionFunction
    – Raul
    Mar 23 '17 at 9:35
  • Hi, I edited my code like this : <apex:form id="actiononload"> <apex:actionFunction name="loadData" action="{!loadData}" status="myStatus" oncomplete="generateImageDoc();" reRender="actiononload"/> </apex:form> , but I have the same result Mar 23 '17 at 9:36
  • generateImageDoc() is a JavaScript function? If it is apex, you need to call another actionFunction to invoke it
    – Raul
    Mar 23 '17 at 9:37
  • no it's an apex method that I want to invoke when loadData ends, in this way I have to separate transaction Mar 23 '17 at 9:43
4

You're missing rerender in your apex:actionFunction, without which whole page would reload.

Your JavaScript code looks fine.

Have a dummy apex:OutputPanel for rerendering.

I suspect your second function call is a apex method, in that case you can call another apex:actionFunction to invoke it. Because oncomplete only accepts JavaScript function.

<apex:outputPanel id="dummy"/>
<apex:actionFunction name="loadData" 
    action="{!loadData}"  
    status="myStatus" 
    oncomplete="callGenerateImageDoc();"
    rerender="dummy"/>

<apex:actionFunction name="callGenerateImageDoc" 
    action="{!generateImageDoc}"  

rerender="dummy"/>

Added:

Change JavaScript function to:

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

You're missing parenthesis in both the function call.

1

There are couple of problems in your code.

  1. Your JavaScript. It has error in it. See your Console.

  2. As @Rahul pointed out, you've not added reRender attribute to your actionFunction.

Use jQuery or simply use JavaScript code as @Rahul suggested and it will work. Refer below for example:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <apex:actionFunction action="{!loadData}" name="loadData()" reRender="panel1" oncomplete="generateImageDoc();" />
     <script>
        var j$ = jQuery.noConflict();

        j$(document).ready(function() {
            alert('load');
            loadData();


        });
     </script>
5
  • Hey @Rohit, i don't think that is the issue over here. the JavaScript method should work fine. I could be wrong though :)
    – Raul
    Mar 23 '17 at 9:48
  • @RahulSharma Yes. He needs to add reRender condition in actionFunction too Mar 23 '17 at 9:49
  • yup. have added it already. thanks for pointing out! Keep up the good work mate.
    – Raul
    Mar 23 '17 at 9:53
  • @RahulSharma There is problem in his JavaScript code too. I tried his code on my developer org and it doesn't work. He should try something like this :window.onload = function() {load1();}; Mar 23 '17 at 9:57
  • window.onload = function onload() { alert('hi'); } - this works. He is just missing a parenthesis in his function call.
    – Raul
    Mar 23 '17 at 10:06
0

You can use apex page action to fulfill your requirement. In Action method use first action and from the first action you can call the second action.

<apex:page controller="CrashDossierPDFController" renderAs="pdf" applyBodyTag="false" applyHtmlTag="false" showHeader="false" action="{!loadData}">

Public void loadData(){
 your functionality...
 then call the second function 
 generateImageDoc();
}
4
  • I can't because I need to separate transcation, in the first one I make a callout and in the second one I save a record Mar 23 '17 at 9:30
  • @L.benedettini did you tried changing script the way I posted as an answer? Mar 23 '17 at 9:31
  • Yes Rohit Mourya , thank you but did not solved the problem the methods seem not start at all, I put System.Debug in the first rows to inspect, but nothing Mar 23 '17 at 9:34
  • L.benedettini, Did you add attribute reRender each time you used <apex:function>? .Without reRender attribute, this tag will be totally ignored.
    – SF Learner
    Mar 23 '17 at 21:39

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.