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
RockersRockers 

How to close the current open window?

Hi Friends,

 

            In my VF Page, we used two buttons "Save" and "Close". In Page, we are dispalying the current contacts for an Account.

 

          When Clicking "Save"; the details are going to be stored/updated.

 

         when clicking "Close"; the current window which is showing the details, has to be closed.

 

         How to do that. I used javascript for this button on Event = onclick="closeWindow();"

 

         I wrote the script as follows:

 

          <script>

         function closeWindow(){
            window.close();
        }

</script>

 

       But, it is not working. Please suggest me, if any solutions are there.

 

       Thanks in advance.

 

Regards,

Phanikumar

 

Juan SpagnoliJuan Spagnoli

I don't think that is a good idea to close the window when user hits the button, because you can miss error messages. Mmmm.. If I were you, I'll use a extension controller to perform the save and set a Boolean flag to indicate close's action. After save, when your VF is refreshed, you'll do:

 

<script language="javascript">
//At the end of the form, before </apex:form>

if({!closeWindow}){
   window.close();
}
</script>

 

The controller:

 

public with sharing class MyController {
        private ApexPages.StandardController con;
        public Boolean closeWindow {get;set;}

	public MyController(ApexPages.StandardController stdController){
	     con = stdController;	
	}

        public PageReference save(){
             try{
                  con.save();
                  closeWindow = true;
             }catch(Exception e){
                  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage())); 
             }
             return null;
        }        
}

 

 

And make sure that you have <apex:pageMessages/> at the beginning of the page.

 

Regards.

RockersRockers

Hi Friend,

 

        Thank you for your reply. I used the code which you replied to my query. But, in Save, i used the flag variable and i updated the code as said by you. After clicking save; validations are done,  but, the window is not going to close.

 

Please see the code below:

 

Controller:

 

    public pageReference save(){
        
        Integer count = 0;
        if(literatureOrderItemsList.size()>0){
            update literatureOrderItemsList;
            closeWindow=true;
            message = 'Items Updated Successfully';
            displayMessage = true;
           
        }

 

In the page also, i wrote the script before </apex:form> tag as you provided.

 

But, it is not working. After clicking save, it is showing the same page with validation messages. Please suggest me how to do this.

 

Regards,

Phanikumar

Juan SpagnoliJuan Spagnoli

Could you paste your visualforce's code, please?

 

(Please uses "Insert Code" button in your reply)

 

Thx!

RockersRockers

Hi Friend,

 

      Please see the VF Page code .

 

<apex:page>
	<apex:form >
		<apex:pageblock rendered="{!displayMessage}">
			<apex:outputText value="{!message}"/>
		</apex:pageblock>
		
		<apex:pageBlock rendered="{!displayGrid}">        	
        	<apex:pageBlockButtons >
            	<apex:commandButton value="Submit" action="{!submit}"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockTable var="list" value="{!literatureOrderItemsList}" border="2">
                <apex:column width="15">
	                <apex:facet name="header">Literature Order Item</apex:facet>
	                <apex:outputText value="{!list.Name}"/>
                </apex:column>
                <apex:column >
	                <apex:facet name="header">Order Quantity</apex:facet>
	                <apex:outputText value="{!TEXT(list.Order_Quantity__c)}"/>
                </apex:column>
                <apex:column >
	                <apex:facet name="header">Order Status</apex:facet>
	                <apex:inputField value="{!list.Order_Status__c}"/>
                </apex:column>
                <apex:column >
	                <apex:facet name="header">Shipment Tracker</apex:facet>
	                <apex:inputField value="{!list.Shipment_Tracker__c}"/>
                </apex:column>
                 
            </apex:pageBlockTable>
		</apex:pageBlock>
		<script language="javascript">
			if({!closeWindow}){
			   window.close();
			}
		</script>
	</apex:form>
</apex:page>

 Regards,

 Phanikumar

Juan SpagnoliJuan Spagnoli

But... There is not "standardController", "controller" or "extensions" in <apex:page> tag.. How are you linking the page to the controller?

 

By the way.. You can use <apex:pageMessages> for render messages to the user, in the controller you code:

 

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'My message'));

  Also, you can use "ApexPages.Severity.Warning" for another different level of severity.

RockersRockers

Sorry Friend,

 

           I missed the page information.

<apex:page controller="UpdateItems" showheader="true">
	<apex:form >
		<apex:pageblock rendered="{!displayMessage}">
			<apex:outputText value="{!message}"/>
		</apex:pageblock>
		
		<apex:pageBlock rendered="{!displayGrid}">        	
        	<apex:pageBlockButtons >
            	<apex:commandButton value="Submit" action="{!submit}"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockTable var="list" value="{!literatureOrderItemsList}" border="2">
                <apex:column width="15">
	                <apex:facet name="header">Literature Order Item</apex:facet>
	                <apex:outputText value="{!list.Name}"/>
                </apex:column>
                <apex:column >
	                <apex:facet name="header">Order Quantity</apex:facet>
	                <apex:outputText value="{!TEXT(list.Order_Quantity__c)}"/>
                </apex:column>
                <apex:column >
	                <apex:facet name="header">Order Status</apex:facet>
	                <apex:inputField value="{!list.Order_Status__c}"/>
                </apex:column>
                <apex:column >
	                <apex:facet name="header">Shipment Tracker</apex:facet>
	                <apex:inputField value="{!list.Shipment_Tracker__c}"/>
                </apex:column>
                 
            </apex:pageBlockTable>
		</apex:pageBlock>
		<script language="javascript">
			if({!closeWindow}){
			   window.close();
			}
		</script>
	</apex:form>
</apex:page>

Regards,

Phanikumar

Damien_Damien_

You cannot close the current window if it was NOT opened by javascript somewhere else because of security issues with your browsers.  If you opened the current page with javascript all you need to do is:

 

<apex:commandButton onclick="window.close();" value="Close" />

 

Juan SpagnoliJuan Spagnoli

Please add <apex:pageMessages/> after the <apex:form> tag. And please enclose your save method with a try-catch block like I post you as example.

 

How do you open the new window? Are you using a custom button for a record layout?

 

thx.