2

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">
  <head>
    <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>
    <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.Date__c}','{!child2.Rating_Type__c}',
               '{!child2.Rating__c}', '{!JSENCODE(child2.Comments__c)}',
               '{child2.Reference_Status_stoplight__c}'],
           </apex:repeat>
      ]);

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


    } 

    google.setOnLoadCallback(drawVisualization2);
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="ratings"></div>

  </body>
</apex:page>   

1 Answer 1

5

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}'

1
  • 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! Mar 10, 2014 at 22:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .