function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to create a popup window using Apex code


How do you create/display a small popup windows right in the middle just to inform the users some information and when a user click Ok the popup window will go away? Please advice. Please send sample codes if available.






I'm not sure about Apex, but I know it's possible with Javascript, which visualforce supports.  When do you want the pop-up to occur?


You might try doing a quick google search for javascript notifications.



I found the Javascript below that works fine now. However how do you apply the following command in Visualforce? Basically I want if the criteria is evaluated to true it then will display it otherwise it wil not.


Response.Write("<script language="JavaScript"> { var x = confirm ('Congratulation! '); } </script>")


Please advice. thanks


You'll need to write the condition such that the VF criteria is understood by the JavaScript, e.g.



Response.Write("<script language="JavaScript"> { if ("yes"=="{!IF(true, 'yes', 'no')}") {var x = confirm ('Congratulation! ');} } </script>")


In this case the 

"{!IF(true, 'yes', 'no')}"

gets turned into "yes" by the time the page is rendered, thus the JavaScript condition evaluates to true and the confirmation dialog is displayed.

Imran MohammedImran Mohammed

As you need to alert a popup window with confirmation message.

You can try this


<script language="JavaScript">


    confirm('{!IF(controllerVariable == 'yes', 'Confirmation', 'anyothermessage')}');



This will prompt irrespective of whether the condition is true or false




<script language="JavaScript">

    if('{!controllerVariable}' == 'yes')
This will prompt only if variable in controller has the value 'yes'




controllerVariable is a variable defined in Apex controller.


I feel the second approach will be better for you.


Thanks for all your tips.


Imran MohammedImran Mohammed

Did you finally got the popup window working now?


Yes the popup window works now. Thanks.


Hi, could you tell me how you did this? I'm trying to do the same thing using a scheduled class. So far I have something like:



global void execute(SchedulableContext ctx) {
public PageReference callPage(){
	PageReference pageRef =  new PageReference('/apex/somepage');
	return pageRef;



But I know the second method is wrong. :(


I'm afraid you won't be able to do this from a scheduled class, for the simple reason that there is no browser-based user request associated with it.


The scheduled job runs on the server at a particular time, thus there is no user to return any information to.


Are you trying to send some information about the scheduled job to a user?  If that is the case, I'd suggest a notification email.


No I was trying to pop up a custom notification about tasks, but what you said makes sense. I kind of figured it would be something like that. Thanks for the confirmation!

Shri RajShri Raj

I'm trying to craete a popup where users can input or chosse numbers from 1-10 (Kind of picklist selection) Please suggest. Thanks