Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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>   
share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
    
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 Mar 10 at 22:26
add comment

Your Answer

 
discard

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.