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
mauricio.ramos@corpitalmauricio.ramos@corpital 

Issue with google chart in VF page

HEllo,

 

I havea visualforce page that I am trying to create a bar chart. I am using js remoting to call a method in the controller to get the result data BUT it is giving me a Null reference error when trying to access the account variable in the controller to use as a query parameter in the @RemoteACtion method.

 

 

CVF page:

<apex:page extensions="GoogleChartsController" sidebar="false" standardController="Account" showHeader="false" pageStyle="Account"  > 
    <apex:includeScript id="a" value="https://www.google.com/jsapi" />     
    <script type="text/javascript">
        // Load the Visualization API and the piechart package.
        google.load('visualization', '1.0', {'packages':['corechart']});
        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(initCharts);
        //load all the charts..
        function initCharts() { 
              GoogleChartsController.loadChildCases(function(resultCase, event){  
                  var visualization = new google.visualization.ColumnChart(document.getElementById('caseChart'));
                  var data1 = new google.visualization.DataTable();  
                  data1.addColumn('string', 'Cases');
                  data1.addColumn('number', 'Cases');       
                  //add rows from the remoting results
                  for(var i =0; i < resultCase.length; i++){
                      var r = resultCase[i];
                      data1.addRow([r.Id, r.expr0]); 
                   }
                    // all done, lets draw the chart with some options to make it look nice.
                    visualization.draw(data1, {legend : {position: 'top', textStyle: {color: 'blue', fontSize: 10}}, width:150,vAxis:{textStyle:{fontSize: 10}},hAxis:{textStyle:{fontSize: 10},showTextEvery:1,slantedText:false}});
              }, {escape:true});
              
          } 
    </script>
     <div id="caseChart" />
</apex:page>

 And this is the controller:

global class GoogleChartsController {
 global static Account acc; 
 
    public GoogleChartsController(ApexPages.StandardController controller) {
        acc = (Account)controller.getRecord();
    }

    @RemoteAction   
    global static AggregateResult[] loadChildCases() {
        AggregateResult[] caseLst = [Select ParentId, count(Id) FROM Case  WHERE AccountId = :acc.id Group By ParentId ];
        return caseLst;
    }
}

 

Please let me know where I am making an error in this.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Taiki YoshikawaTaiki Yoshikawa

Hi,

 

I think that can

 

VF Page

function initCharts() { 
              GoogleChartsController.loadChildCases('{!acc.Id}', function(resultCase, event){

 Controller

@RemoteAction   
    global static AggregateResult[] loadChildCases(Id prmAccountId) {
        AggregateResult[] caseLst = [Select ParentId, count(Id) FROM Case  WHERE AccountId = :prmAccountId Group By ParentId ];
        
        return caseLst;
    }

 

All Answers

Taiki YoshikawaTaiki Yoshikawa

Hi,

 

I think that can

 

VF Page

function initCharts() { 
              GoogleChartsController.loadChildCases('{!acc.Id}', function(resultCase, event){

 Controller

@RemoteAction   
    global static AggregateResult[] loadChildCases(Id prmAccountId) {
        AggregateResult[] caseLst = [Select ParentId, count(Id) FROM Case  WHERE AccountId = :prmAccountId Group By ParentId ];
        
        return caseLst;
    }

 

This was selected as the best answer
mauricio.ramos@corpitalmauricio.ramos@corpital

YESSS that did the job, I was actually trying to get the class variable value from inside the remote method when I could just have passed it as a parameter. 

 

THANK YOU!!  Accepting as solution.