6

Suppose I am redirected from page 1 to page 2 due to some action performed on VF page . I am looking for a way to use a hyper link in page 2 clicking on that will redirect me back to page 1 . Anyone can help me out with some hints!! thanks.

  • Please try to use tags with a close relation to the content of your question. – Samuel De Rycke Dec 30 '13 at 10:40
7

For standard pages Salesforce implemented a nice pattern: when redirecting the source page passes parameter retURL that tells the target page where to return. For example when you choose to Edit a contact from the account layout, Salesforce takes you to

https://<instance>.salesforce.com/<Contact ID>/e?retURL=%2F<Account ID>

and redirects back to the account after saving changes or canceling.

The retURL param can be easily accessed from both APEX:

ApexPages.currentPage().getParameters().get('retURL')

and VisualForce:

{!$CurrentPage.Parameters.retURL}

BTW when getting retURL in apex you should enclose ist in a try catch block. Becuase sometimes a user can directly go to that page in which case there will be no retURL. If an exceptio0n occurs then it means that no retURL parameter is present then you can just redirect to page you want by creating a custom PageReference.e.g. new PageReference('/home/home.jsp');

Of course there are other options, for example playing with HTTP Referer.

| improve this answer | |
  • 2
    Hi Endrju...I need to get the previous page for a VF page and I am trying to access this value in APEX code .So, should I go ahead with {!$CurrentPage.Parameters.retURL}? Could you pls specify how to get the value of 'retURL' ? – user4644 Dec 29 '13 at 11:06
  • From the APEX code you should go with the first option: ApexPages.currentPage().getParameters().get('retURL'). ApexPages.currentPage.getParameters() simply gives an access to all parameters passed through URL. – endrju Dec 29 '13 at 11:16
  • Hi, my base URL is - c.cs10.visual.force.com/apex/myPage?id=a2NJ00000003OqO. After some illegal action I am showing an error message to the user and there I kept a hyperlink to the error message added at apex pagemessage section. At this also the URL is same as base url..Only refreshing the browser is able to show me the base record..Pls suggest how to inject the refresh browser code in my hyperlink of error message. – user4644 Dec 29 '13 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy