You need to sign in to do that
Don't have an account?
TehNrd
If URL param contains & symbol all params are lost
I have no idea if this is the correct forum to post this in as it affects apex, visualforce, and sites. If the url parameter value contains the & symbol all parameters of the URL will be lost. For example:
PageReference page1 = new PageReference('http://www.test.com/page?name=Anderson And Smith'); system.debug(page1); OUTPUT: System.PageReference[http://www.test.com/page?name=Anderson+And+Smith]
PageReference page2 = new PageReference('http://www.test.com/page?name=Anderson & Smith'); system.debug(page2); OUTPUT: System.PageReference[http://www.test.com/page]
URL encode the & symbol you might say. When I do this the symbol gets double encoded and then this causes issues:
PageReference page3 = new PageReference('http://www.test.com/page?name=Anderson %26 Smith'); system.debug(page3); OUTPUT: System.PageReference[http://www.test.com/page?name=Anderson+%2526+Smith]
I'm sure part of the issue is that the & symbol is the designated seperator for URL params but there must be a way to adresss this problem, right?
Any ideas or solutions?
Thanks,
Jason
Jason, you'll probably have to put the parameter manually into the PageReference by using the getParameters().put('id','value') method.
Try this:
Output: 11:45:0.623|USER_DEBUG|[9,9]|DEBUG|System.PageReference[/apex/UrlParamTest?name=Anderson+%26+Smith]
There is an example in the ApexCode reference.
-Matt
Polymorphism speaks the truth!
But if you're look to build one long string you can encode the value (or any bit really) using the following:
Wes
Jason,
You do have to do the encoding as the other post suggests. I'll be interested in hearing what your outcome is.
I've had this issue with & and " symbols. I've done the URL Param trick on custom buttons and on VF pages. The one issue that I've run into -- regardless of the encoding - is browser specific.
I have a VF that passes over a lot of information in the URL Params. Including account names, subject etc. This works most of the time. However, I've found that I have users on Firefox and Chrome where the encoding simply doesn't work. I run into this same issue when encoding a series of Params to pre-fill a report for example: (pv0={!Case.name})
Again it's browser specific, it works flawlessly with IE, but breaks for any Firefox or chrome users. The fun part it only breaks if there's an odd symbol like the ampersand.
I am going to bring this post back from the dead.
The whole reason behind this thread is I was looking to build some type of URL shortener. The problem was that if the URL contained an "&" symbol within one of the parameter values it would get all messed up and break, see original post. There is the Link Force app on the AppExchange but from what I can tell that uses a URL field to store the long URL. This has a limit of 255 characters and in my use case this wont work. What did work was using this line of code to add URL params. It did not double encode characters and all was good.
My use case also had about 20 url params. So how to do this? Let's say we want to create this url http://www.mysite.com?name=jason&company=Cool & Rad.
I created a custom object with two fields, Base URL (Text) and URL Params (Long Text).
The Base URL field would contain this value: http://www.mysite.com
The long text field stores the URL params like this:
The code below would contruct this URL:
This will break if by some random chance a URL param value contains "<---------------->" but I am willing to bet against this ever happening but you can always make the delimiter something even more random if this is a concern.
-Jason
URL encoding, please refer to:
http://www.w3schools.com/tags/ref_urlencode.asp
http://meyerweb.com/eric/tools/dencoder/
I have run into this problem before as well. If you are trying to achieve this from visualforce, this is what I use:
This will take care of ampersands and also in my case single apostraphes because I am using javascript for the url redirect and passing dynamic course names that have plural names and use ampersands made that pretty difficult.Cheers!