I have to show a message on top of a visual force on load page,if certain conditions are met.

I know 2 ways:

1) in the constructor:

    if (Querycondition){
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Warning,'warning description') 

2) use action attribute of apex:page

<apex:page standardController="Account"  action="{!CheckValidation}">

and in the controller:

public PageReference CheckValidation(){     
            if (Querycondition){
                 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Warning,'warning   description') 
        return null;   

Is there some difference in terms of performance?

Thanks in advantage for any advice.

3 Answers 3


I can think of no reason why there should be a significant difference in performance.

Doing it in the constructor is the simplest approach. And you won't have to make a separate call in your tests to run that code.

The page action runs after the constructor has completed and has the capability of redirecting to another page which is not used here. So it seems a little over-complicated for this case. (It might be a useful approach if you want to use the same controller with multiple pages, where some need the check and others don't.)


There is a 3rd way (anyway). Which completes the above adding separation between logic and content.

If the error/message is always the same, i.e. is not dynamically created in APEX, and the condition to show it is already "packed" in a boolean or a getValue() boolean method within your controller, then you could insert an apex:pageMessage with error message ready straight into the Visualforce page applying it a rendered attribute as required by the init context (created after constructor execution - back to first option)


There is another way. Since your validation seems to do a query, there is a possibility that the query could take some time, till which point the user sees a blank white page.

Instead you could have a the basic page load and then in the windows.onload event, call a actionFunction, which will call the Apex method. Meanwhile you could show an ActionStatus telling the user that the page is still alive.

<apex:actionStatus startText="Loading.." id="statusId" />
    <apex:actionFunction name="checkValidation" action="{!CheckValidation}" status="statusId" rerender="somesection">

If your query is very simple one then this approach might not be necessary, but will helps in cases where your logic might be a bit complex.

You must log in to answer this question.

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