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
Ken_KoellnerKen_Koellner 

How to return to prior page?

If I want a method in Apex controller to return to the prior page, what should I return for the page reference?

 

I would assume I can return a URL with the id that was passed in but that would open a new instance of a page.  I'd rather just go back.

 

shillyershillyer

Take a look at this example. It's the controller for an 3-page Opportunity wizard. It includes the methods for page navigation.

 

Hope that helps,

Sati

Ken_KoellnerKen_Koellner

But that example doesn't really answer the question.  I want to go back to the page I came from and that example goes to the page of the new object created.

 

 

shillyershillyer

This is the code that navigates between pages from the example:

 

// The next three methods are used to control navigation through // the wizard. Each returns a reference to one of the three pages // in the wizard. public PageReference step1() { return Page.newOpptyStep1; } public PageReference step2() { return Page.newOpptyStep2; } public PageReference step3() { return Page.newOpptyStep3; }

 

 

 

And then if you look at the Visualforce page for the second page of the wizard, you'll notice these the 2 command buttons:

 

 

<apex:page controller="newOpportunityController" tabStyle="Opportunity"> <apex:sectionHeader title="New Customer Opportunity" subtitle="Step 2 of 3"/> <apex:form> <apex:pageBlock title="Opportunity Information"> <apex:facet name="footer"> <apex:outputPanel> <apex:commandButton action="{!step1}" value="Previous" styleClass="btn"/> <apex:commandButton action="{!step3}" value="Next" styleClass="btn"/> </apex:outputPanel> </apex:facet> <apex:pageBlockSection title="Opportunity Information"> <apex:panelGrid columns="2"> <apex:outputLabel value="Opportunity Name" for="opportunityName"/> <apex:inputField id="opportunityName" value="{!opportunity.name}"/> <apex:outputLabel value="Amount" for="opportunityAmount"/> <apex:inputField id="opportunityAmount" value="{!opportunity.amount}"/> <apex:outputLabel value="Close Date" for="opportunityCloseDate"/> <apex:inputField id="opportunityCloseDate" value="{!opportunity.closeDate}"/> <apex:outputLabel value="Stage" for="opportunityStageName"/> <apex:inputField id="opportunityStageName" value="{!opportunity.stageName}"/> <apex:outputLabel value="Role for Contact: {!contact.firstName} {!contact.lastName}" for="contactRole"/> <apex:inputField id="contactRole" value="{!role.role}"/> </apex:panelGrid> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

 

 

Hope that helps,

Sati

Ken_KoellnerKen_Koellner

I get how that works but I want to return to the page prior to when I got in my VF page.

 

Example:

 

I put a customer button on an Opportunity page layout that calls a VF page.

 

The user hits a button on my VF page.  The button calls my controller which does some work and then passes the user back to the page they came from (like they hit the back arrow in their browser).

 

(Assuming I have the opportunity id pass in as a parameter and saved in a variable called oppId)

 

I know I can do --

    Opportunity opp = [select id from Opportunity where id = :oppId];
    ApexPages.standardController controller = new ApexPages.standardController(opp);

or

      PageReference oppPage = new PageReference('/' + oppId);
      oppPage.setRedirect(true);

 

But that opens a new page for the opportunity.  It doesn't go back.

 

Maybe what I'm asking just can't be done.

 

Ken_KoellnerKen_Koellner

Would I be able to return a page reference iwth a javascript in it?

 

Something like-- 'javascript:history.back()'

RyanhRyanh
I think you're on the right track, but I would include in the code for the button a parameter like returnUrl which passes the URL of the opportunity page (or whatever page you're on). Then your VF page would pull that parameter from the URL, save it as a property of your custom controller and make it available to the pageReference that you return after the operation is complete... make sense?