function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TehNrdTehNrd 

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

 

 

Matthew KeefeMatthew Keefe

Jason, you'll probably have to put the parameter manually into the PageReference by using the getParameters().put('id','value') method.

 

 

Try this:

    public void getUrl() {
        PageReference page1 = new PageReference('/apex/UrlParamTest');
        page1.getParameters().put('name','Anderson & Smith');
        system.debug(page1);
    }

 

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.

 

getParameters Map<String, String>
Returns a map of the query string parameters that are included in the page URL. The key string contains the name of the parameter, while the value string contains the value of the parameter. This map can be modified and remains in scope for the PageReference object. For instance, you could do:
PageReference.getParameters().put('id', myID);

 

-Matt

WesNolte__cWesNolte__c

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:

 

 

String myUrl = domain + context + key + EncodingUtil.urlEncode('Anderson & Smith','UTF-8');

 

Wes

 

BrianWKBrianWK

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.

TehNrdTehNrd

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.

 

page1.getParameters().put('name','Anderson & Smith');

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:

 

name<---------------->jason
company<---------------->Cool & Rad

 

The code below would contruct this URL:

 

 

List<URL_Shortener__c> url = [select Base_URL__c, URL_Parameters__c from URL_Shortener__c where Id = :ApexPages.currentPage().getParameters().get('id')];
        
if(url.size() > 0){
    page = new PageReference(url[0].Base_URL__c);
       
List<String> paramList = url[0].URL_Parameters__c.split('\n'); //Loop through param and add to page for(String s : paramList){ List<String> keyValue = s.split('<---------------->'); page.getParameters().put(keyValue[0],keyValue[1]); }
}

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

Trophy ChenTrophy Chen
Please use the URL encoding.
 
String name='Anderson & Smith';
name=name.replace('&', '%26');
page.getParameters().put('name',name);

URL encoding, please refer to:
http://www.w3schools.com/tags/ref_urlencode.asp
http://meyerweb.com/eric/tools/dencoder/

 
Kenneth KimbrellKenneth Kimbrell

I have run into this problem before as well. If you are trying to achieve this from visualforce, this is what I use:

JSENCODE(SUBSTITUTE(string_here,"&","%26"))
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!