You need to sign in to do that
Don't have an account?
How to prevent browser back re-executing
I'm using a dummy Visualforce page with an action on the apex:page to allow me to connect a custom button with exectuting some Apex code. Everything works fine, as per the many examples.
Here's the page:
<apex:page standardController="Opportunity" extensions="CloneOppButtonController" action="{!autoRun}"> <apex:sectionHeader title="Auto-Running Apex Code"/> <apex:outputPanel > You tried calling Apex Code from a button. If you see this page, something went wrong. You should have been redirected back to the record you clicked the button from. </apex:outputPanel> </apex:page>
Here's the relevant bits of the extension controller:
ublic with sharing class CloneOppButtonController { private final ApexPages.StandardController theController; public CloneOppButtonController(ApexPages.StandardController stdController) { theController = stdController; } public PageReference autoRun() { Id cloneId; // Call the Apex Method we want to invoke and then show the new page try { cloneId = CloneOpportunity.cloneWithProducts(theController.getId()); } catch (Exception e) { theController.getRecord().addError('Opportunity could not be cloned, check debug log for errors'); } // New page is id of the cloned object if it worked, or previous page otherwise if (cloneId == null) cloneId = theController.getId(); return new PageReference('/' + cloneId); }
However, when a user clicks their browser Back button the dummy page is the previous page in the browser history and so it executes again - and creates another clone of the opportunity. As the destination page is a standard Salesforce page, there's little I can do on that - I can't guarantee control over the page layout as this is part of a package being developed.
I've seen on other forums about trying to use javascript to manipulate the browser history but this doesn't seem to fire before the action method.
Any other solutions ?
try setting setRedirect on PageReference to true. this issue also occurs in http post situations. Search for PRG (Post Redirect Get) to read on that more.
All Answers
try setting setRedirect on PageReference to true. this issue also occurs in http post situations. Search for PRG (Post Redirect Get) to read on that more.
Thanks, worked a treat.