7

I want to call public apex method on load of visual force page, for purpose of Excel exporting.

But I don't want to put that method in Constructor of apex class, because of some limitation (example:I need to query all the records and leading to too many soql exception).

Basically Visual Force page has some criteria, so I want to call this after setting criteria. Run the report and export as Excel.

How can take approach for calling certain apex method from on load of Visual force page.

8

You can use the action attribute of the page tag for this - that will execute an action method on the controller before anything is rendered. E.g.

Controller:

public class MyController
{
   public void MyActionMethod()
   {
      // do some stuff here
   }
}

Page:

<apex:page controller="MyController" action="{!MyActionMethod}">

<!-- rest of page -->

</page>

One important point to note - this should not be used to initialise information that other parts of the page rely on, as there is no guarantee that this method will be called before the getters (my understanding is that is how it currently works, but may change in the future) - there's a thread with one of the Visualforce developer's explaining this in more detail at:

https://developer.salesforce.com/forums?id=906F0000000957bIAA

If this causes a problem, the other way is to execute some JavaScript when the page loads that invokes the action method and then renders the rest of the page. I've blogged that solution at:

http://bobbuzzard.blogspot.co.uk/2011/08/dml-during-initialisation.html

3

The order of visualforce execution is first constructor and then your getters run and if you have any action method ,then your setters are called before your action method begins .

Also In visualforce there is no guarantee of which getter method executes first if multiple getters found and same for setters.So you need to explicitly call those methods if your business logic demands

There is an Action attribute method in the apex:page attribute and for your case i feel you should use that and check filter in this method

<apex:page action="{!yourmethod}">

The Action methods are similar to onload function in javascript and executes on your page load .

  • 2
    The action method on the page attribute isn't guaranteed to be executed before other methods, the only guarantee is that the page won't be rendered until it completes, as it may result in a server-side redirect - here's a thread with one of the visualforce developers explaining in a little more detail: developer.salesforce.com/forums?id=906F0000000957bIAA – Bob Buzzard Jan 10 '14 at 8:26
  • @BobBuzzard Thanks for letting me know this .But currently i have seen lot of managed apps using same principle but you are right may break at any point as per your link – Mohith Shrivastava Jan 10 '14 at 8:39
  • Yeah, lots of people think its safe because it currently works - when the VF developers tell me its not future proof that's good enough for me to avoid it :) – Bob Buzzard Jan 10 '14 at 8:40

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.