function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Thakkar ParthThakkar Parth 

SCRIPT1007: Expected ']' error

I'm getting an error while displaying Highcharts . The error is : SCRIPT1007: Expected ']' at line 47,character 4. Pressing my head since hours and trying to solve but no luck yet :( .Really appreciate the help.Thanks in advance. Marc


==== Class ===

public class HighChartsController3
{
  public String[] total;
  public String[] gettotal()
  {
     String q = 'SELECT count(Id),Name Total FROM Account group by Name';
     AggregateResult[] agr =Database.query(q);
     String[] strarray = new String[agr.size()];
     for (AggregateResult ar : agr) 
     {
         strarray.add(String.valueof(ar.get('Total')));

     }            

     return strarray ;
   }


public String[] position;
public String[] getposition()
{

     String q = 'SELECT count(Id),Name FROM Opportunity group by Name';
     AggregateResult[] agr =Database.query(q);
     String[] strarray = new String[agr.size()];

      for (AggregateResult ar : agr)
      {
         strarray.add(string.valueOf(ar.get('Name')));
     }

     return strarray ;
     }
   }



    === Page ===

<apex:page controller="HighChartsController3">
  <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" />
  <script src="https://code.highcharts.com/highcharts.js"></script>
  <script src="https://code.highcharts.com/modules/exporting.js"></script>

   <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

  <Script>
    $(function () {
     $('#container').highcharts({
     chart: {
        type: 'column'
    },

    title: {
        text: 'Total # of Apllications vs # of Open Jobs'
    },

    xAxis: {
     categories: {!position}
    // categories: ['abc','parth','hello']        

    },

    yAxis: {
        allowDecimals: false,
        min: 0,
        title: {
            text: 'Number'
        }
    },

    tooltip: {
        formatter: function () {
            return '<b>' + this.x + '</b><br/>' +
                this.series.name + ': ' + this.y + '<br/>' +
                'Total: ' + this.point.stackTotal;
        }
    },

    plotOptions: {
        column: {
            stacking: 'normal'
        }
    },

    series: [{
        name: 'New',
        data: [5, 3, 4, 7, 2],
        stack: 'Application'
    }, {
        name: 'Schedule Interviews',
        data: [3, 4, 4, 2, 5],
        stack: 'Application'
    }, {
        name: 'Hired',
        data: [3, 4, 4, 2, 5],
        stack: 'Application'
    },{
        name: 'Rejected',
        data: [3, 4, 4, 2, 5],
        stack: 'Application'
    },

      {
        name : 'Total Applications',
        data : {!total}           
        //data : [100,200,300],
        stack : 'Total'
        }]
      });
   });

  </script>
</apex:page>


Prafull G.Prafull G.
Hi Marc,

There are couple of changes you need to do
On visualforce page - Place a comma after {!total}
{
        name : 'Total Applications',
        data : {!total},         
        //data : [100,200,300],
        stack : 'Total'
        }]
      });

The main change is in controller class. When you are returning list of String its coming as it on vf page without single quotes that will represent it as string on script. We are adding it additionally.
LINE 11
strarray.add('\''+ String.valueof(ar.get('Total')) + '\'');

LINE 29
strarray.add('\'' + string.valueOf(ar.get('Name')) + '\'');
Let us know if it helps.

Cheers,
Prafull
Thakkar ParthThakkar Parth
I made your changes but  still getting an error which is  : SCRIPT1004: Expected ';' at line 33 . I dont see any need to add semi colon . Strange !
Prafull G.Prafull G.
Yes. Its working at my end too. I think one glitch can be special character i.e. ' in account names that may be causing string break. If it is so then try replacing signle quote with double quote in controller.

i.e.
strarray.add('\"' + string.valueOf(ar.get('Name')) + '\"');