4

Past performance: A visualforce that would save and then close would hit a validation rule and would then close before the user could see the Message on screen.

I resolved this by putting a inputHidden on the screen with the value of a string in my controller... Now I cannot get the popup to close because the value is never "Saved" even though the items are saving.

How do I get the value in the controller to the page so that the Javascript can let the window close after the save happens

Page:

<apex:page standardcontroller="Learning_Review__c" extensions="LearningReviewPageController" showHeader="false" sideBar="false" action="{!initReviewPage}">

<apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-1.10.2.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-ui-1.10.4.custom.min.js')}"/>
<apex:stylesheet value="{!URLFOR($Resource.jQuery, '/css/ui-lightness/jquery-ui-1.10.4.custom.css')}"/>

<apex:pageMessages id="msg"/> 
<apex:form id="createReview">
<apex:pageBlock title="Review for {!LIName}?" mode="edit">

    <apex:pageBlockButtons ><apex:outputPanel id="buttons">
        <apex:commandButton rendered="{!reviewed=FALSE}" value="Add Review" action="{!saveReview}" status="closer" rerender="statusMsg, buttons, msg"/>
        <apex:commandButton value="Cancel" onclick="window.close()"/>
        <apex:actionStatus startText="(Thank you for your review.)" stopText="" onStop="MyCloseWindow();" id="closer">
            <apex:facet name="start">
                <apex:outputPanel style="margin: 0 10px;">
                    <apex:image url="/img/loading.gif"/>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionStatus>
        </apex:outputPanel>
    </apex:pageBlockButtons> 

    <apex:pageBlockSection rendered="{!reviewed=FALSE}" title="Review Information" columns="1" id="pbs">
            <apex:outputPanel id="RatingImport" >
                <apex:outputLabel value="Your Rating" for="RatingRepeat"/>
                <apex:repeat value="{!Nodes}" var="node" id="RatingRepeat">
                    <apex:commandLink action="{!SaveRating}" rerender="RatingImport" title="{!node.Rights__c}">
                        <apex:param name="Location" assignTo="{!Location}" value="{!node.Isbn__c}" />
                        <apex:image url="{!node.lmscons__Version__c}" />
                    </apex:commandLink>
                </apex:repeat>
            </apex:outputPanel>
            <apex:inputfield id="title" value="{!Learning_Review__c.Review_Title__c}"/>
            <apex:inputTextarea id="review" value="{!Learning_Review__c.Review__c}" rows="10"/>
            <apex:inputfield id="anon" value="{!Learning_Review__c.isAnon__c}" label="Make Comments Confidential"/>
            <apex:inputHidden id="statusMsg" value="{!reviewedMsg}"/>
            <script>
                var status = document.getElementById('{!$Component.statusMsg}');
            </script>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<script type="text/javascript"> 
function MyCloseWindow() {

        alert(status.value);
        if(status.value == 'Saved'){
            window.opener.refreshTable();
            self.close();

        }
    }
</script>

The Controller:

 public LearningReviewPageController(apexpages.Standardcontroller con){
    master_sc = con;
    location = 0;
    newReview = (Learning_Review__c)con.getRecord();
    reviewedMsg = 'Not';
}
 // save the new rating and reload the page
public PageReference SaveRating() {
    CurrentRating = Location;
            system.debug(' \n ----------> CurrentRating in SaveRating = ' + CurrentRating);
    getNodes();
    reviewedMsg = 'Saved';        
    return null;
}

public PageReference saveReview(){ 
    try{
        insert newReview;
        reviewed = true;
        reviewedMsg = 'Saved';
    }
    catch (dmlException e) {
        reviewedMsg = 'Not';
        ApexPages.addMessages(e);
    }

    return null;
}
1

The problem is that your status variable is getting wiped out when the page is rerendered. That's because the elements are ripped out of their position in the DOM and replaced with the new content returned from the server. While the new elements appear identical to the old, except for the updated contents, they actually occupy different memory references, and the original element is discarded before your function is called.

You must query the status element within MyCloseWindow to make sure that the DOM element you are querying is the one that will have the updated information. Moving that single line of code to within the MyCloseWindow function should give you the desired effect.

  • That didn't work. I removed the one on the page and then tried again. Looking at the browser console I can see that the hidden field is not updating – SeanGorman Jul 30 '14 at 13:51
  • I added the line back in, so now have it twice and rerendered the field when the stars were selected. Now validation rules fire and screen only closes when there is no exception. – SeanGorman Jul 30 '14 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.