I have created this simple visualforce page, using google charts. Everything works great until there is a comment in the rating with an apostrophe ('). I have tried using the JSENCODE, but it is still not working - any suggestions?

The below works fine with no apostrophes - but renders nothing when there is one. (in the child2.Rating_Type__c field.

<apex:page standardController="Account" showheader="false">
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="//www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['table']});
    <script type="text/javascript">
       function drawVisualization2() {
      // Create and populate the data table.
         var data2 = google.visualization.arrayToDataTable([
          ['Contact Name', 'Date', 'Rating Type', 'Rating', 'Comments', 'Rating Status'],
           <apex:repeat value="{!Account.Product_Ratings__r}" var="child2">

               ['<a href="http://na13.salesforce.com/{!child2.id}">    {!child2.Contact_Name_Text__c}</a>',
               '{!child2.Rating__c}', '{!JSENCODE(child2.Comments__c)}',

      // Create and draw the visualization.
      visualization = new google.visualization.Table(document.getElementById('ratings'));
      visualization.draw(data2,{allowHtml: true, page: 'enable', pageSize: '25'});


  <body style="font-family: Arial;border: 0 none;">
    <div id="ratings"></div>


The simplest approach would probably be to encapsulate it in double quotes so that you don't need to worry about escaping a string with single quotes in it.

e.g. "{!child2.Rating_Type__c}" instead of '{!child2.Rating_Type__c}'

| improve this answer | |
  • 1
    I did notice that I had the wrong field name - should be '{!JSENCODE(child2.Comments__c)}'. Updated the code above. I tried "{!child2.Comments__c}" worked perfectly. Thanks! – Merry Stambaugh Mar 10 '14 at 22:26

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.