+ Start a Discussion
JP BlonshineJP Blonshine 

Need HELP with Too Many Script Statements

Greetings All!!

 

I have been working on a Google Visualization and am working on creating a graph with several plot points. The error I am getting is: Content cannot be displayed: Too many script statements: 200001

 

I am working with plotting out 2305 records into a line graph.

 

1) Is this possible?

 

2) If so how can I avoid the limitation?

 

Here is the Code:

public with sharing class TestResultGraphConroller {

	
	public TestResultGraphConroller(ApexPages.StandardController stdController){
	}
	
	public String getTestResult(){
        return getTestResult(null);
    }
    
    public String getTestResult(List<Id> ids){
        
        GoogleViz gv = new GoogleViz();
                
        gv.cols = new list<GoogleViz.col> { 
            new GoogleViz.Col('col1','Channel Flow','number'),
            new GoogleViz.Col('col2','Channel Volume','number'),
            new GoogleViz.Col('col3','Test','string')
        };
                
        Decimal numTestResult = 0.01;
        string query = 'SELECT Id, Channel_Flow__c, Channel_Volume__c, Test__c FROM Trial_Data__c WHERE Channel_Volume__c > 0' ;
                         
        // The unit testing path.
        if(ids != null && ids.size() > 0){
            query += ' AND Id IN :ids';
        }
        
        query += ' ORDER BY Name ASC'; 
                       
        for(Trial_Data__c td : Database.query(query)){  
            GoogleViz.row r = new GoogleViz.row();
            r.cells.add ( new GoogleViz.cell( numTestResult ) );
            r.cells.add ( new GoogleViz.cell( td.Channel_Volume__c ) );
            r.cells.add ( new GoogleViz.cell( td.Channel_Flow__c ) );
            r.cells.add ( new GoogleViz.cell( td.Test__c ) );
                
            gv.addRow( r );
            numTestResult ++;
        }

        return gv.toJsonString();
    } 

 Thank you for any help you can provide!!

 

JP

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

Your problem is most likely in this block:

 

 

 for(Trial_Data__c td : Database.query(query)){  
            GoogleViz.row r = new GoogleViz.row();
            r.cells.add ( new GoogleViz.cell( numTestResult ) );
            r.cells.add ( new GoogleViz.cell( td.Channel_Volume__c ) );
            r.cells.add ( new GoogleViz.cell( td.Channel_Flow__c ) );
            r.cells.add ( new GoogleViz.cell( td.Test__c ) );
                
            gv.addRow( r );
            numTestResult ++;
        }

 This loop is running 2305 times and driving you over the governor limit.  You essentially need to save script lines and then limit the SOQL at the highest number possible.  Unfortunately, I don't see what else you can do offhand.  What I would do first is find out what is the largest number of records that you can do (ie.  LIMIT 1000, LIMIT 1500, etc.) and then determine if that is acceptable.  

All Answers

steve456steve456

give Limit 1 in the soql query

JP BlonshineJP Blonshine

Are you saying change this:

string query = 'SELECT Id, Name, Channel_Flow__c, Channel_Volume__c, Test__c FROM Trial_Data__c WHERE Channel_Volume__c > 0' ;

  To this?

string query = 'SELECT Id, Name, Channel_Flow__c, Channel_Volume__c, Test__c FROM Trial_Data__c WHERE Channel_Volume__c > 0' LIMIT 1;

 If so, that does not work as only one point on my graph will get plotted out.

 

Regards,

 

JP

Jake GmerekJake Gmerek

Your problem is most likely in this block:

 

 

 for(Trial_Data__c td : Database.query(query)){  
            GoogleViz.row r = new GoogleViz.row();
            r.cells.add ( new GoogleViz.cell( numTestResult ) );
            r.cells.add ( new GoogleViz.cell( td.Channel_Volume__c ) );
            r.cells.add ( new GoogleViz.cell( td.Channel_Flow__c ) );
            r.cells.add ( new GoogleViz.cell( td.Test__c ) );
                
            gv.addRow( r );
            numTestResult ++;
        }

 This loop is running 2305 times and driving you over the governor limit.  You essentially need to save script lines and then limit the SOQL at the highest number possible.  Unfortunately, I don't see what else you can do offhand.  What I would do first is find out what is the largest number of records that you can do (ie.  LIMIT 1000, LIMIT 1500, etc.) and then determine if that is acceptable.  

This was selected as the best answer
JP BlonshineJP Blonshine

I actually did a combination of things to resolve it. I first started down your path Jake by doing the limit. Ultimately, I was able to reduce the amount of queries by seperating them into different extensions rather than doing it all at once. Thank you for the input. It directed me to the resolution!!

 

Regards,

 

JP 

Jake GmerekJake Gmerek

I'm glad I could help, and happy that you have gotten it resolved.  SF governor limits are one of the more difficult aspects of Apex to get used to dealing with.