2

I have a force.com sites set up and am using an iframe on our website to display a VF page. After record submission I want to redirect the parent window to a Thank You page. So far I have this working nicely by having the controller set a gotoThankYouPage boolean to true and return null, then in the VF page a section like this handles the redirect:

<apex:outputPanel rendered="{!gotoThankYouPage }">
   <script>
      window.top.location='http://sample.webqa.net/partners/portal/marketing/thank-you/';
   </script>
</apex:outputPanel>  

The problem I'm having is that if the user hits the browser back button from that Thank You page, it resubmits the record and redirects them to the Thank You again. I can see that if you hit back, the goThankYouPage is still true, so I would expect the user to get pushed to the Thank you again. My question is why would the submit button code be triggered just by going back?

I changed the design to redirect the user to a new VF page that has the Thank You content instead of the parent window location change - here if you click Back it just displays the original page with data filled in, but does not resubmit - as expected. My client prefers the parent window approach though - any ideas why that would resubmit?

Controller:

public without sharing class PartnerCoopFundController {

@testVisible private Contact PartnerContact {get; set;}
public boolean validContact {get;set;}
// page errors collected
public string pgErrors {get; set;}
public Co_Op_Fund__c coop {get; set;}

// set boolean after save is success to tell page to redirect parent to Thank You
public Boolean gotoThankYouPage {get; set;}

public PartnerCoopFundController() {
    coop = new Co_op_fund__c();
    validContact = false;
    gotoThankYouPage  = false;
    // do we have contact Id in URL for page
    if (ApexPages.currentpage().getparameters().get('contactId') == null)
        pgErrors = 'We had trouble setting up the Co-op Fund Request page; please contact your account manager at Cumulus for assistance.';
    else {
        // check if value is Id and exists in SF
        try {
            Id contactId = ApexPages.currentpage().getparameters().get('contactId');
            PartnerContact = [select id, name, accountid, account.name, email from Contact where id = :contactId];
            coop.Requester_Name__c = PartnerContact.id;
            coop.Partner__c = PartnerContact.AccountId;
            coop.Requester_Email__c = PartnerContact.Email;
            validContact = true;
        } catch (Exception e) {
            pgErrors = 'We had trouble finding your account details; please contact your account manager at Cumulus for assistance.';
        }
    }
}

// save the Fund Request record and redirect
public PageReference saveFundRequest() {
    try {
        upsert coop;
        // set boolean so page knows to redirect user to Thank You - URL is in the VF page button
        gotoThankYouPage = true;
    } catch (Exception e) {
        pgErrors = 'We had a problem saving your Fund Request.  Please try again or contact your account manager at Cumulus for assistance.';
    }
    return null;

}

}

VF Page:

<apex:page controller="PartnerCoopFundController" docType="html-5.0" standardstylesheets="false" showheader="false" sidebar="false" id="pg">

<!-- Global CSS files -->
<apex:stylesheet value="{!URLFOR($Resource.CCC_Reset_CSS)}" />
<apex:stylesheet value="{!URLFOR($Resource.CCC_html5boiler_ZIP, 'css/main.css')}" /> 

<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' /> 
<apex:stylesheet value="{!URLFOR($Resource.CCC_Styles_CSS)}" />
<apex:stylesheet value="{!URLFOR($Resource.CCC_Styles_Responsive_CSS)}" />

<style>
    table{width: 100%;}
    td{min-width: 225px;}
    <!-- remove invalid styling on reqd fields -->
     input:invalid, textarea:invalid { background-color: #F5F5F5  !important;  } 

</style>
<apex:form id="form">        

    <div id="HomePage" class="container">
        <div class="AccountUserPage_Content HomePage_Content">

            <div class="span9 push left">
                <div class="Acc-description">
                   <!-- error text section -->
                   {!pgErrors}
                </div>
                <apex:outputPanel rendered="{!validContact}">

                    <div class="Acc-description">

                        <table id="tbl_partnercoopfundrequest" class="primary-acc-table edit-user-table" style="border: 0;">
                            <tr>
                                <td class="licenseTable">
                                    <div class="label">Activity Name</div> 
                                    <div class="content"><apex:inputField value="{!coop.Activity_Name__c}" styleClass="inputFieldsCustom" required="true"/></div>
                                </td> 
                                <td class="licenseTable"> 
                                    <div class="label">Activity Type</div> 
                                    <div class="content"><apex:inputField value="{!coop.Activity_Type__c}" styleClass="inputFieldsCustom" required="true"/></div>  
                                </td>
                            </tr>
                            <!-- more fields removed for brevity -->
                            <tr>
                                <td class="licenseTable">
                                    <div class="label">Total Budget (US Dollars)</div> 
                                    <div class="content"><apex:inputField value="{!coop.Total_Budget__c}" styleClass="inputFieldsCustom"  required="true"/></div>  
                                </td> 
                                <td class="licenseTable"> 
                                    <div class="label">Requested Amount (US Dollars)</div> 
                                    <div class="content"><apex:inputField value="{!coop.Requested_Funds_Amount__c}" styleClass="inputFieldsCustom"  required="true"/></div>  
                                </td>
                            </tr> 

                            <tr> <td rows="2"> <apex:commandButton action="{!saveFundRequest}" value="Submit"  styleclass="cccBtn" /></td></tr>                                                                                                                                                                                            
                        </table>

                    </div> 
                </apex:outputPanel>  <!-- No page errors -->                 
            </div> <!-- left content -->
        </div> <!-- HomePage_Content -->
    </div> <!-- HomePage -->
</apex:form>   

<apex:outputPanel rendered="{!gotoThankYouPage }" id="panel">
   <script>
      window.top.location='http://client-name.qa.net/partners/portal/marketing/thank-you/';
   </script>
</apex:outputPanel>             

0

Seems to me that your issue might need to debugging into the code you haven't posted. But as to resolve your issue, if there is no big reason, you can try using window.open('http://sample.webqa.net/partners/portal/marketing/thank-you/', '_parent') in your VF page.

| improve this answer | |
  • Thanks Lance, I added the code and VF. I switched the script to use window.open but am still getting a second Coop Fund record created when the user clicks back. The debug logs just look exactly the same as the one when I actually do click the submit button. – nikiV Jul 13 '15 at 12:21
0

It's not a best solution but i would suggest you to set cookie to your browser. So you can control it before(above) redirection between your script tags.

<script>
   function getCookie(c_name) {
     var i, x, y, ARRcookies = document.cookie.split(";");
     for (i = 0; i < ARRcookies.length; i++) {
         x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
         y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
         x = x.replace(/^\s+|\s+$/g, "");
         if (x == c_name) {
             return unescape(y);
         }
     }
  }

  if(getCookie('thankyou')!='true'){
      var d = new Date();
      d.setTime(d.getTime() + (1*24*60*60*1000));//this will set cookie to expire after one day
      var expires = "expires="+ d.toUTCString();
      document.cookie = 'thankyou=true;'+expires+';'
      window.top.location='http://client-name.qa.net/partners/portal/marketing/thank-you/';
  }


</script>
| improve this answer | |

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.