Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using apex:outputLink or URLFOR(target, id, params) is great in Visualforce. But in Apex land I always see people looping through lists using EncodingUtil.urlEncode(param, 'UTF-8').

If System.Url doesn't have a parameter parser or builder, what's the best way to do it?

share|improve this question
up vote 10 down vote accepted

This capability is available using the PageReference class. You can use the same mechanism to build query strings or parse the parameters out of one, without loops and a whole lotta script statements.

Parse an RFC 3986 URL query string

String query = '?password=Hunter2dog%26cat&';

System.PageReference pageReference = new System.PageReference('/' + query);
Map<String,String> parameters = pageReference.getParameters();

String password = parameters.get('password'); //Hunter2dog&cat
String username = parameters.get('username'); //

Build an RFC 3986 URL query string

Map<String,String> parameters = new Map<String,String>{
  'password' => 'Hunter2dog&cat',
  'username' => ''

System.PageReference pageReference = new System.PageReference('');

String query = pageReference.getUrl();

share|improve this answer
Thanks. That is what I was looking for. – Patlatus Apr 9 '15 at 7:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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