+ Start a Discussion

redirect to previous page



I am trying to create a save and return button for my custom edit page and I am struggling to get a page reference that will return the user to the previous page.  What method do I use to return the Previous Page?

Rahul SharmaRahul Sharma

You want to redirect to previous page means?

Please elaborate a bit.


I think you have to save every page you visit in some variable. And in method that handle go back on previous page you should set this (previous) page as current and return it. Smth like this.


To elaborate I need to current visualforce page I am on to redirect to the previous page.  The user gets to this edit page of of my custom object page named "Job".


I found this method, but how do I apply it to return to the previous URL?



Rahul SharmaRahul Sharma

For that when you are redirecting user to your edit page pass the previous page name along with it as a parameter.

suppose ur previous page name is Referer, then pass it as a parameter.



then in ur edit page, after save, redirect your page to previous page.

now you will get the page name from the current page url (since you pass the page name in previousPage variable)


 hope it helps!


I am a little bit confused by this response, can you highlight what you can in the code below so that I can see an example?  This edit page is reached from a related list on the job custom object.


edit page visual force


<apex:page standardController="Job__c" extensions="Incent">
<apex:form >
<apex:pageBlock title="Edit Incentives">
		<apex:pageBlockButtons > 
			<apex:commandButton value="Save & Return" action="{!SaveReturn}" reRender="incentpanel"/>  
        	<apex:commandButton value="Save" action="{!save}" reRender="incentpanel"/> 
        	<apex:commandButton value="New" action="{!NewIncentive}" reRender="incentpanel"/>
     	<apex:pageblockSection >
                 <apex:outputField value="{!Job__c.Account__c}"/>
                 <apex:outputField value="{!Job__c.Job_Name_Subject__c}"/>
                 <apex:outputField value="{!Job__c.Name}"/>
     <apex:pageblockTable value="{!Incents}" var="incent" id="incentpanel">
             <apex:column headerValue="QTY">
                 <apex:inputField value="{!incent.QTY__c}"/>
             <apex:column headerValue="Description">
                 <apex:inputField value="{!incent.Description__c}"/>
             <apex:column headerValue="Honoraria__c">
                 <apex:inputField value="{!incent.Honoraria__c}"/>
             <apex:column headerValue="Deleted">
                 <apex:inputField Value="{!incent.Deleted__c}"/>



 edit page extension class


public with sharing class Incent {
public list<AIIncentive__c> Incents {get; set;}
public list<job__c> job {get; set;}
public id jobid;
public string NewIncentive {get; set;}
public PageReference refresh = ApexPages.currentPage(); 

    public Incent(ApexPages.StandardController controller) {
     	jobid = apexpages.currentpage().getParameters().get('id');
        system.debug('################################## Id'+jobid);

        Incents = [Select QTY__c, Description__c,  Honoraria__c, Job__c, Deleted__c  From AIIncentive__c  where Job__c = :jobid]; 
        job = [SELECT Account__c FROM Job__c WHERE id= :jobid];    

    public PageReference NewIncentive(){
    	AIIncentive__c obj = new AIIncentive__c();

          	obj.QTY__c = 0;
     		obj.Description__c = '';
     		obj.Honoraria__c = 0;
     		obj.Job__c = jobid;

	 	update Incents;
     	insert obj;
   	 	return refresh;
    public PageReference save() {
    	update Incents;

        return refresh;

//    public PageReference SaveReturn() {
//    	update Incents;
//    	return 
//    }


Rahul SharmaRahul Sharma

how are you redirecting to this edit page?

if custom code, please post the code.


the code that i posted is what the edit button redirects too

Rahul SharmaRahul Sharma

The page from where you are redirected to this edit page is custom or standard?


it is a standard page, but custom object

Rahul SharmaRahul Sharma

you have overidden the edit button with you page, so there you need to pass the page name as a parameter.


so I would have to create a custom page for the object I am getting to this edit page from?