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

Alert on VF Page - then redirect?



I am calling a VF page from a Custom Button. I have successfully managed to display an alert to the user if certain conditions are met. How can I redirect the user to the original page that had the custom button after this message is displayed? I am displaying the message using the following:-


 <script language = "JavaScript">
   var msg = 'There is no valid Contact Record or Contact Role - You may not send an Email until this is done.'
   if ('{!validtoflag}' == 0) {
   	alert (msg);


 Many thanks






You can open the vf page as a child window. After displaying the alert you can close the childe window and reload your parent window where you have your custom button.


Try the below code snippet as reference:


<apex:page >


function closeme1()







<apex:form >

  <apex:commandButton value="close me and referesh" onclick="closeme1()"/>






Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 






You are having a custom button on an object's layout which means for e.g. If your page is "Testpage" and on Custom button you have defined like to redirect the user to "/apex/TestPage?Id={!Account.Id} ", it will automatically pass the account Id to your VF page... So in the javascript you need do the below.



// put the below stmt. after your validation and the alert message.'../' + '{!$CurrentPage.Parameters.Id}');



when you click the custom button, you can set the retURL parameter, after redirecting to visualforce page, you can use it in javascript code by assign the retURL to an page context variable.


Hi Rossco,


Try this code


<apex:page controller="GeneratePPI" action="{!insert1}" showHeader="false" sidebar="false">
<apex:form >
<apex:outputPanel rendered="{!errormsg}">
   <script type="text/javascript">
     window.parent.location.href ="/{!$}";
    window.alert("This Puchase Order is Already inserted in PPI "); }

errormsg is boolean type declare this class. True this boolean type in your method and declare this as false in constructor.