+ Start a Discussion

System.LimitException: Too many script statements: 1000001 in Class



Hi All,


I have a urgent issue to be resolved. I have migrated my code into the production environment.

The code moved successfully in to the production area. But an issue arised on executing the piece of code in production.


I have written a batchable class to avoid governer limits, using the queryLoacator. Even then the issue isn't resolved.


Please enlighten me why such issues arise and provide some information on governor limits and how one could bypass them when bulk data query is required.


Please find my piece of code below.

global class Get_Account_Id_EPC implements Database.Batchable<sObject>
    Public String Query;

    Global  database.querylocator start(Database.BatchableContext BC){
        Return database.getquerylocator(query);

    Global void execute(Database.BatchableContext BC, List<SObject> scope)
        List<Express_Payment__c> updatelist = new List<Express_Payment__c>();
        List<Express_Payment__c> epsobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from Express_Payment__c limit 1000];

        for(Express_Payment__c records : epsobj){
            for(sObject s : scope){
                Account a = (Account)s;
                if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == a.BPID__c && records.BP_Loc_Id__c == a.BP_Loc_Id__c){
                    records.Agent__c = a.id;
        List<Utility_Bill_Payment__c> updatelist1 = new List<Utility_Bill_Payment__c>();
        List<Utility_Bill_Payment__c> ubpobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from Utility_Bill_Payment__c limit 1000];

        for(Utility_Bill_Payment__c records : ubpobj){
            for(sObject s : scope){
                Account b = (Account)s;
                if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == b.BPID__c && records.BP_Loc_Id__c == b.BP_Loc_Id__c){
                    records.Agent__c = b.id;
        List<PrePaid_Card__c> updatelist2 = new List<PrePaid_Card__c>();
        List<PrePaid_Card__c> ppcobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from PrePaid_Card__c limit 1000];

        for(PrePaid_Card__c records : ppcobj){
            for(sObject s : scope){
                Account c = (Account)s;
                if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == c.BPID__c && records.BP_Loc_Id__c == c.BP_Loc_Id__c){
                    records.Agent__c = c.id;
        List<MT_Volume_Reports__c> updatelist3 = new List<MT_Volume_Reports__c>();
        List<MT_Volume_Reports__c> mtannualobj = [Select  id,Agent__c,BPID__c,BP_Loc_Id__c from MT_Volume_Reports__c limit 1000];

        for(MT_Volume_Reports__c records : mtannualobj){
            for(sObject s : scope){
                Account d = (Account)s;
                if(records.BPID__c!=null && records.BP_loc_ID__c!=null&&records.BPID__c == d.BPID__c && records.BP_Loc_Id__c == d.BP_Loc_Id__c){
                    records.Agent__c = d.id;
        List<MT_Volume__c> updatelist4 = new List<MT_Volume__c>();
        List<MT_Volume__c> mtobj = [Select  id,Agent__c,BPID__c,BP_Loc_Id__c from MT_Volume__c limit 1000];

        for(MT_Volume__c records : mtobj){
            for(sObject s : scope){
                Account e = (Account)s;
                if(records.BPID__c!=null && records.BP_loc_ID__c!=null&&records.BPID__c == e.BPID__c && records.BP_Loc_Id__c == e.BP_Loc_Id__c){
                    records.Agent__c = e.id;
        List<MO_Volume__c> updatelist5 = new List<MO_Volume__c>();
        List<MO_Volume__c> moobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from MO_Volume__c limit 1000];
        for(MO_Volume__c records : moobj){
            for(sObject s : scope){
                Account f = (Account)s;
                if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == f.BPID__c && records.BP_Loc_Id__c == f.BP_Loc_Id__c){
                    records.Agent__c = f.id;

    Global void finish(Database.BatchableContext info){

    /* Test Coverage Class*/
    Public static testMethod void SEUpdateTest()
        User currentUser = [Select u.Id From User u Where u.Id =: UserInfo.getUserID()];
        List<Account> testAccs = new List<Account>();
        for(integer i=0;i<10;i++)
            Account testAccount = new Account(Name = 'TestAccount'+i, OwnerId = currentUser.Id);
        insert testAccs;
        Get_Account_Id_EPC sObj = new Get_Account_Id_EPC();
        sObj.Query = 'Select id,BPID__c,BP_Loc_Id__c from Account';
        ID BatchId = Database.executeBatch(sObj,500);
        AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id = :BatchId];
        System.debug('\n\nFinal results are: '+a);
        //System.AssertEquals('Completed', a.status);
        //System.AssertEquals(0, a.NumberOfErrors);



Above is my batchable class.


Using below code i invoke the batchable class :

    public void exec6(){
        Get_Account_Id_EPC epcQuery = new Get_Account_Id_EPC();
        epcQuery.Query = 'Select id,BPID__c,BP_Loc_Id__c from Account';
        ID batchprocessid = Database.executeBatch(epcQuery);


The issue arises when i tr to execute the code in production giving out an error System.LimitException: Too many script statements: 1000001, Pointing to the following line:


if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == f.BPID__c && records.BP_Loc_Id__c == f.BP_Loc_Id__c)


I am failing to understand why this error is occuring. Please help me with the same.


I am running out of time :(

Any pointers are appreciated.





Best Answer chosen by Admin (Salesforce Developers) 
Thanks a Lot for Your reply. It helped a lot. I have modified the query to reduce the number of rows retrieved as: List epsobj = [Select id,Agent__c,BP_ID__c,BP_Loc_Id__c from Express_Payment__c where BP_ID__c!= null and BP_Loc_Id__c!=null and Agent__c =: null limit 1000]; and it worked.

All Answers


In order to prevent infinite loops or very inefficient iterative database calls, the script execution framework throws a runtime exception if too many script statements are executed without any database statements. For more information, see Execution Model and Governors. 


so within your for loop  if there are too many rows (more than the limit) that doesn't satisfy your if condition ,it will fail..


I think if you move your if condition to soql where clause it might work.  



if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == b.BPID__c && records.BP_Loc_Id__c == b.BP_Loc_Id__c){

This could be included in the soql as  -> 


List<Utility_Bill_Payment__c> ubpobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from Utility_Bill_Payment__c  where bp_ID_C !=null and bp_loc_id__C !=null .....];


Then you don't need the if condition and fewer scripts will be run ..


hope this helps!




did that help?


Thanks a Lot for Your reply. It helped a lot. I have modified the query to reduce the number of rows retrieved as: List epsobj = [Select id,Agent__c,BP_ID__c,BP_Loc_Id__c from Express_Payment__c where BP_ID__c!= null and BP_Loc_Id__c!=null and Agent__c =: null limit 1000]; and it worked.
This was selected as the best answer

Please help me I am getting the error 


System.LimitException: Too many script statements: 200001


Class.ProductPriceBook1.: line 39, column 18 External entry point     


Here is my code.

public class ProductPriceBook1 {

    public class productInfo {
        public Product2 prod{get; set;}
        public List<PriceBook2> priceBooks {get; Set;}
        public productInfo() {
        priceBooks = new List<PriceBook2>();
    public List<ProductInfo> products {get; set;}
    public ProductPriceBook1() {
        products = new List<productInfo >();
        List<Product2> prods = [Select Name, Description, Family, id from Product2  limit 1000];
        List<Id>prodIds = new List<Id>();
        for (Product2 p : prods)
        List<PricebookEntry> pEntries = [Select id, product2Id, pricebook2id from PricebookEntry where product2Id in :prodIds ];
          List<id> pbIds = new List<Id>();
        for (PriceBookEntry pe : pEntries)
        Map<Id, Pricebook2> priceBooks = new Map<id, Pricebook2>([Select id, name from PriceBook2 where id in :pbIds ]);
        for (Product2 prod : prods ) {
            ProductInfo pInfo = new ProductInfo();
            pInfo.prod = prod;
          for (PricebookEntry pe : pEntries)  {
                 if (pe.product2Id == prod.id) {
            products.add(pInfo );


<apex:page controller="ProductPriceBook1">
    <apex:pageBlock >
         <apex:pageblockTable value="{!products}" var="product">
             <apex:column value="{!product.prod.name}" />
              <apex:column >           
                  <apex:repeat value="{!product.pricebooks}" var="priceBook">
                      {!priceBook.name} <br/>
                <apex:column value="{!product.prod.Description}" />
                 <apex:column value="{!product.prod.Family}" />           

 Please help