0

I have a function inside my apex controller :

public List<Decimal> getcheckProductMaxMinPrice(){
    List<Decimal> listDeci = new List<Decimal>();
    listDeci.add(5.0);
    listDeci.add(7.0);

        System.debug('Called ! ');

    return listDeci;

}

When I call it from my JS function inside my VF page :

<apex:commandLink action="{!addToTable}" oncomplete="doneAdding();" />

function doneAdding(){
   console.log("DEBUG : ",{!checkProductMaxMinPrice});

}

on my debug log in apex :

The only one call to my apex function "getcheckProductMaxMinPrice" is when I reloading the page.

When I hit the commandLink button - my apex function is never called.

Which means that the " System.debug('Called ! ');" will be printed on the screen only when I refresh the page (Calling the constructor) but not when I hit the commandLink button and the line console.log("DEBUG : ",{!checkProductMaxMinPrice}); in my JS function is not calling the function even though I have a get before my function name.

So actually I have 2 questions here :

  1. Why the function in my apex controller is called when I reload the page, if I call it ONLY when I hit the commandLink button?

  2. Why this line : console.log("DEBUG : ",{!checkProductMaxMinPrice}); does not call the function in my apex Controller?

Thanks!

  • 1
    Is your javascript rendered before your method is called? if you inspect the element what value do you see... – Girbot May 30 '19 at 15:02
1

Why the function in my apex controller is called when I reload the page, if I call it ONLY when I hit the commandLink button?

The code is called only when the page is rendered.

Why this line : console.log("DEBUG : ",{!checkProductMaxMinPrice}); does not call the function in my apex Controller?

It did; when the page was rendered.


Your primary problem is assuming that the Apex method is called when the JS method is called, which is not correct. The Apex method is called before the page renders or re-renders. And, since you did not specify a re-render, the entire page reloaded, so the oncomplete method does not get a chance to execute.

The minimum changes you need are to add a reRender target, and that target should be the script in question. Here's what that looks like:

<apex:commandLink action="{!addToTable}" oncomplete="doneAdding();" reRender="scriptTag"/>

<apex:outputText id="scriptTag">
   <script>
    function doneAdding(){
      console.log("DEBUG : ",{!checkProductMaxMinPrice});
    }
  </script>
</apex:outputText>
| improve this answer | |
  • Thank you ! Actually, I have rerendered attribute there - It's not specified here by mistake - but I have few more rerenders and that why it happened before. Thanks for the explanation it helped me to understand something I probably didn't understand correctly. – Salvation Jun 3 '19 at 8:06

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.