Contents[Hide]

Read URL parameters values

Example URL:

/apex/myVisualforcePage?orderId=a0ZL000000053VSMAY&companyName=Mike%20%26%20John%20M%C3%BCller

Where "Mike%20%26%20John%20M%C3%BCller" is UTF-8 URL encoded CompanyName "Mike & John Müller". It contains "&" character which if not encoded will breake URL and German natinal characer "ü" which we want to preserve to avoid unexpected convertion to another character.

Get parameters values by name:

String orderId = ApexPages.currentPage().getParameters().get('orderId');
String companyName = EncodingUtil.urlDecode(ApexPages.currentPage().getParameters().get('companyName'),'UTF-8');

Empty and null values.

You will get empty string value when URL is like that:

/apex/myVisualforcePage?orderId=&secondparam=

Both paramers will be equal to ''

orderId == ''
secondparam == ''

You will get null value when parameter is missing

/apex/myVisualforcePage?secondparam=
orderId == null
secondparam == ''

Before you make a query which search for this order make sure that parameter is not empty or null.

Order__c orderObject;
String orderId = ApexPages.currentPage().getParameters().get('orderId');

if (orderId != null && orderId != '') {        
    orderObject = [SELECT Id, Name FROM Order__c WHERE Id = :orderId];
} else {
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Missing Order Parameter'));
}

Generate URL with parameters.

public PageReference doCreateNewSalesChannel() {
    String paramValue = EncodingUtil.urlEncode('Mike & John Müller', 'UTF-8');

    // PageReference pr = new PageReference('URL'); // any URL, may lead to external site.
    // PageReference pr = Page.MyCustomVisualforcePage; // Existing VS page
    pr.getParameters().put('paramName','paramValue'); // parameters.
    pr.setRedirect(true);
    return pr;
}

Resources:

EncodingUtil Class description

Helpfull online app which helps to learn about encoding see http://coderstoolbox.net/string/

   
© Paweł Woźniak