i have two params in apex,

errorNum = 1;
errorMessage = System.Label.UPL_OFFMSG_OfferExist;

now,i want to redirect from the current page to another,but the param will lost in the next page.

please help.

  • Try to use the same controller in both pages and setRedirect(false) More info – Sergej Utko Sep 19 '13 at 6:36

The key to this is to use the PageReference class and in particular the getParameters() method.

I will make the presumption that the page you are trying to redirect to is a VF page called NextPage.

The first step is to get a PageReference reference for that page using PageReference pr = Page.NextPage;

You can now make use of the goodness of the PageReference class as documented in the Salesforce docs.

The method that we're interested in is the getParameters() method. As you can see from the documentation this returns a map of parameters that you can add to. Which means you can add your variables to the map to make sure they are passed as parameters in the URL:

pr.getParameters().put('errorNum', errorNum);
pr.getParameters().put('errorMessage', errorMessage);

The PageReference class then converts these parameters to Key Value pairs in the query string of the URL that it builds.

If you want to force the browser to do a client side redirect (use GET rather than POST) then use pr.setRedirect(true); and then return the Page Reference from your method.

A small note: If you're redirecting to an external page you will need to construct the PageReference by using the target URL. And in this case the browser will always be forced to do a redirect, so setRedirect become superfluous.

    Pagereference p=new Pagereference('/apex/samplepage2?errorNum =1&errorMessage =System.Label.UPL_OFFMSG_OfferExist');

    return p;

Pass as a query param as illustrated above

  • I think you should remove & symbol before errorNum parameter. I think that is a typo – Pramod Kumar Sep 19 '13 at 7:31
  • i have done that – Pramod Kumar Sep 19 '13 at 7:32
  • thanks !Should work i feel ?Whats your thought? – Mohith Shrivastava Sep 19 '13 at 8:45
  • Actually original string was ('/apex/samplepage2?&errorNum =1&errorMessage =System.Label.UPL_OFFMSG_OfferExist');. I edited your answer and removed & appearing after ?. – Pramod Kumar Sep 19 '13 at 8:52
  • right !It was type from my side !My bad – Mohith Shrivastava Sep 19 '13 at 9:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.