I have a world map in a Visualforce page and once a country is clicked on, then the action is that the user is auto-navigated to another Visualforce page (/apex/Google_Maps_Reports) which has a parameter passed to the URL.

On this Visualforce page, I have a method to the controller on that page which returns a bar chart of all Opportunities where the Account.BillingCountry is the name of the parameter passed to the URL.

In the examples, Canada is working but United States is not. I believe the latter is not working because the URL returns pc0=United+States. Is there any nice code snippet I can use to rectify this or will I have to create a custom formula field on the Account object where all values are one word, and use that instead?

<apex:page >

<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>google.load('visualization', '1', {'packages': ['geochart']});

function drawVisualization() {var data = new google.visualization.DataTable();

 data.addColumn('string', 'Country');
 data.addColumn('number', 'Value'); 
 data.addColumn({type:'string', role:'tooltip', p:{html:true}});var ivalue = new Array();

 **ivalue['CA'] = 'https://cs81.salesforce.com/apex/Google_Maps_Reports_Overview?pc0=canada';**

 ivalue['CA'] = 'https://cs81.salesforce.com/apex/Google_Maps_Reports_Overview?pc0=United States';
  • TL;DR - to avoid Visualforce issues because of a passed parameter that has spaces (such as 'United States', if you are passing a country variable to the URL for example) then you can use encodeURIComponent('United States'); like: cs81.salesforce.com/apex/… Africa'); and in your Apex method you can then use EncodingUtil.urlDecode to decode the encoded parameter. For example: WHERE Opportunity.Account.BillingCountry =: EncodingUtil.urlDecode(ApexPages.currentPage().getParameters().get('pc0'), 'UTF-8')]; – Andy Hitchings Nov 6 '15 at 18:08

You can encode the space (and other characters that have special meaning in a URL) in the JavaScript by using encodeURIComponent:

ivalue['CA'] = 'https://cs81.salesforce.com/apex/Google_Maps_Reports_Overview?pc0='
        + encodeURIComponent('United States');

or in the Apex by using EncodingUtil.urlEncode.

|improve this answer|||||
  • Thanks Keith. I tried: 'cs81.salesforce.com/apex/… encodeURIComponent('United States');' but to no avail. The apex method I have is: public List<Opportunity> getOppsFromGoogleMaps() { return [SELECT Name, Amount, CloseDate, Close_Date_DD_MM_YY__c, Owner.Name, StageName FROM Opportunity WHERE Opportunity.Account.BillingCountry =: ApexPages.currentPage().getParameters().get('pc0')]; } &I'm unclear how I could encode this via the EncodingUtil.urlEncode method...because the URL variable will always be different as pc0 is different? Many thanks – Andy Hitchings Nov 6 '15 at 11:14
  • 1
    @AndyHitchings My comment about EncodingUtil.urlEncode more related to generating the JavaScript URL but I now assume that the JavaScript is hard coded. If the problem is that you are getting 'United+States' back in your getOppsFromGoogleMaps method then you can remove the encoding in that method using EncodingUtil.urlDecode(ApexPages.currentPage().getParameters().get('pc0'), 'UTF-8'). – Keith C Nov 6 '15 at 12:04
  • Thnaks a lot, Keith. In my vf page I have: data.addRows([[{v:'US',f:'USA'},2,'']]); ivalue['US'] = mysalesforceinstance/apex/Google_Maps_Reports_Overview?pc0='+encodeURIComponent('United States'); and in my method I now have: public List<Opportunity> getOppsFromGoogleMaps() { return [SELECT Name, Amount FROM Opportunity WHERE Opportunity.Account.BillingCountry =: EncodingUtil.urlDecode(ApexPages.currentPage().getParameters().get('pc0'), 'UTF-8')]; }, but the URL being returned is still 'pc0=United+States'. Do you have any idea where I am going wrong? Many thanks – Andy Hitchings Nov 6 '15 at 15:59
  • 1
    @AndyHitchings The URL should contain the + but when the query is done in the controller the + should be removed by the EncodingUtil.urlDecode. – Keith C Nov 6 '15 at 17:14
  • Hi Keith - thank you so much - everything is working as expected. I realized that there must have been too many results per the bar chart in the Visualforce page as when I tried it with other examples like 'South Africa', the Opportunities were being displayed. I amended the SOQL with a LIMIT and tried again with 'United States', and it is working now. Thanks a lot, again! – Andy Hitchings Nov 6 '15 at 18:05

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.