[SalesForce] VF Page – get the previous page URL to redirect back

I have overridden detail and edit pages of an object. So to override, the save functionality or the delete functionality – I need to redirect the user back to the page from where she came from. So in my VF page how do I get back the return URL. I want to do redirect from the controller using pageReference.

Thanks in advance.

Best Answer

If you click overriden button and look at your URL, you will see:

apex/yorpage?id=00136000002lG0u for detail
apex/yorpage?retURL=%2F00136000002lG0u for edit

So in your controller read this parameter:

returnURL = ApexPages.currentPage().getParameters().get('id'); or
returnURL = ApexPages.currentPage().getParameters().get('retURL');

And after everything is complete do return:

return new PageReference('/'+returnURL);
Related Topic