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
minkeshminkesh 

too many script : 200001

Hello,

           I have one method which is exporting record over 2000 and when i am clicking on export all button i am facing error.i.e. too many script statement :200001.

         Now i can't optimize the lines so that i want to catch this exception and i want to show message so i thought to put try catch on that method but still i am getting that exception.

 

Here is the code of that method :-

public PageReference exportAll(){

Pagereference pg;
try{
pg = new Pagereference('/apex/TransactionExcelGenerator?accId='+ controllerAccount.Id + '&exportall=yes' + '&listview='+ selectedList + '&sortfield=' + sortFieldName + '&sortfield1=' + sortFieldName1 + '&ordertype=' + orderType );
system.debug('----page---'+pg);
return pg;
}
catch(Exception ex){
Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.INFO, 'Exporting this view is not possible due to large amount of data being accessed. Please either change view filter, select fewer rows to export or use reporting functionality to extract the data required:'+ex.getMessage()));
return null;
}


}

 

Best Answer chosen by Admin (Salesforce Developers) 
Sam27Sam27

Minkesh,

 

What I am guessing is that actually you are passing some parameter to the "TransactionExcelGenerator" page and the controller of that page is throwing that script error. You must find out the ways to optimize the controller of "TransactionExcelGenerator"  

 

you can use Limits.getScriptStatements() to check as how much scripts is been used, Most probably some codes are being run into loop and some point of time they are crossing the 200000 scripts limit.

 

you can use something like the follow in the beginning or end of the loop (in the controller of TransactionExcelGenerator page)

if ((200000 - Limits.getScriptStatements()) < 10000)

break;

 

it will break the looping as soon as script left is less than 10000.

 

*also you can use system.debug('script used --'+Limits.getScriptStatements()) to check out how much script is being used*

All Answers

spraetzspraetz

You cannot catch governor limit exceptions.  The best way to handle this is to use the Limits methods to check how far into your governor limits you are at various points in your code.  If you are getting close to violating them, you should abort your processing and present a message to the end user. 

Sam27Sam27

Minkesh,

 

What I am guessing is that actually you are passing some parameter to the "TransactionExcelGenerator" page and the controller of that page is throwing that script error. You must find out the ways to optimize the controller of "TransactionExcelGenerator"  

 

you can use Limits.getScriptStatements() to check as how much scripts is been used, Most probably some codes are being run into loop and some point of time they are crossing the 200000 scripts limit.

 

you can use something like the follow in the beginning or end of the loop (in the controller of TransactionExcelGenerator page)

if ((200000 - Limits.getScriptStatements()) < 10000)

break;

 

it will break the looping as soon as script left is less than 10000.

 

*also you can use system.debug('script used --'+Limits.getScriptStatements()) to check out how much script is being used*

This was selected as the best answer
dipsdips

Hi,

 

I have a for loop over map having huge records, its giving too many script error.

I want some solution how can i solve this? serving my purpose of custom key (not ids as key)

 

Following is the piece of code:

 

Map<String, List<Account_Participation__c>> map_APWithCountry = new Map<String, List<Account_Participation__c>>();

Map<Id, Account_Participation__c> map_AP = new Map<Id, Account_Participation__c>();

 

map_AP.putAll([Select Id, Event__c, Catalogue_Country__c, Participation_Status__c,First_Timer1__c,Account__c from Account_Participation__c where Event__c = :listEvents]);
        

for(Id apId: map_AP.keySet())
        {
             setValidAccIds.add(map_AP.get(apId).Account__c);    
             String key = String.valueOf(map_AP.get(apId).Catalogue_Country__c) + String.valueOf(map_AP.get(apId).Event__c);
             List<Account_Participation__c> groupedAPs = map_APWithCountry.get(key);              
             
             if (null==groupedAPs)
             {
               groupedAPs = new List<Account_Participation__c>();
               map_APWithCountry.put(key, groupedAPs);
             }           
             
             groupedAPs.add(map_AP.get(apId));  
                 
        }