I have the following code in my visualforce page

   <apex:commandButton id="btnSendI" onclick="something" action="{!Model.displayDoc}"  value="Send " />
   <input type="submit" value="Cancel" class="btn" onclick="window.reload(); return false;" />

The action tag calls a method of the controller

public PageReference displaySelectedDoc(){
    Controller code....
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,  'Error Message'));

What I want here is that instead of the apex page message an alert box would b displayed in the visualforce page. It is a very urgent need. I tried using oncomplete on the button but that is not working either.

  • Do you mean a javascript alert?
    – Nick C
    Commented Jul 19, 2016 at 10:56
  • yes I wanted a javascript alert
    – Rimii
    Commented Jul 19, 2016 at 11:01

3 Answers 3


There's a lot of ways you can do what you want, probably seeing your code most easy of them is not to use a pagereference method, because this will send you to another url

You could try to declare a string variable and set the error text into it, then at "oncomplete" event, just call a javascript method that consists basically on an alert of your string

  • Is there any way I can do it with the page reference method. This is because the button is actually within a popup and the parent page needs to be refreshed once the Send button is clicked. Can't I put the alert within the refreshed page?
    – Rimii
    Commented Jul 19, 2016 at 11:13
  • You can do that with js, and I think all the code turns simpler that way, just check"window.opener.location.reload();" method
    – Agusnovak
    Commented Jul 19, 2016 at 11:59

You can accomplish it in below way:

Declare a variable say errorMessage in controller as:

public String errorMessage {get;set;}

You have to set this variable in apex controller function whenever you need to display error alert.

Non Create a outputpanel in vf page as:

<apex:outputpanel id="panelToRefresh" >

var error='{!errorMessage}';
function alertError(){

Now change your button code as:

<apex:commandButton id="btnSendI" onclick="something" action="{!Model.displaySelectedDoc}"  value="Send " rerender="panelToRefresh" onComplete="alertError();" />

So whenever your controller functions sets errorMessage it will display a javascript alert. Make sure you set errorMessage as blank at first line of your function.


You could do something like this.

  1. Create a new variable of type String called alertMessage (or any other name).
public String alertMessage {get;set;}
  1. Instead of adding the error message as a PageMessage, set it on the new variable.
public PageReference displaySelectedDoc(){
    Controller code....
    alertMessage = 'Error ocurred';
    return null;
  1. On you page, add the following code:
<apex:outputPanel rendered="{!alertMessage} != ''">
    <script type="text/javascript">

You must log in to answer this question.

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