+ Start a Discussion

How to return to previous page

Hello I have a Visualforce page with a standardcontroller and a controller extension. In this there is a pagereference method in which I save a new order-record. I now want to redirect the user to the previous page. How can I do that. I tried it with the following:


public string retPage = ApexPages.currentPage().getParameters().get('retURL');



did not work. The same with


return retPage;


Where is the fault?





Can you just return null;


I believe that bounces you back to the previous page.


No, if I just return null that leaves me on the detail page of the inserted record.





look apex developer guide there is lot of example is there for page ref.


it is simple.


Hi HarryHH,


You were close, the problem is that the 'retUrl' parameter is just the id, you need to provide the full page reference as such depending on the following two use cases:


If you came from a page that had a specific record id selected (aka the url has a ?id=<id> parameter, then your retPage will be:


PageReference retPage = new PageReference('/'+apexPages.CurrentPage().getParameters().get('id')); 

return retPage;


Or if you were on a record directly (aka the url was <salesforceinstanceurl>/<id of object>) then the retPage will be:


PageReference retPage = new PageReference('/'+ ApexPages.currentPage().getParameters().get('retURL'));


Hope this helps