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
SushupsiSushupsi 

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

:smileysad::smileysad:

 

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;
                    updatelist.add(records);
                    break;
                }
            }
        }
        update(updatelist);
        
        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;
                    updatelist1.add(records);
                    break;
                }
            }
        }
        update(updatelist1);
        
        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;
                    updatelist2.add(records);
                    break;
                }
            }
        }
        update(updatelist2);
        
        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;
                    updatelist3.add(records);
                    break;
                }
            }
        }
        update(updatelist3);
        
        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;
                    updatelist4.add(records);
                    break;
                }
            }
        }
        update(updatelist4);
        
        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;
                    updatelist5.add(records);
                    break;
                }
            }
        }
        update(updatelist5);
    }

    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);
            testAccs.add(testAccount);
        }
        insert testAccs;
        Test.starttest();
        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);
        test.stoptest();
        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.

 

- TIA

 

Sushupsi 

Best Answer chosen by Admin (Salesforce Developers) 
SushupsiSushupsi
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

msreekmmsreekm

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!

Sree

 


msreekmmsreekm

did that help?

 

SushupsiSushupsi
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
symantecAPsymantecAP

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)
                prodIds.add(p.id);
       
        List<PricebookEntry> pEntries = [Select id, product2Id, pricebook2id from PricebookEntry where product2Id in :prodIds ];
          List<id> pbIds = new List<Id>();
        for (PriceBookEntry pe : pEntries)
                   pbIds.add(pe.pricebook2Id);
        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) {
                   pInfo.pricebooks.add(priceBooks.get(pe.pricebook2id));
                }
            }
            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:repeat>
                </apex:column>  
                <apex:column value="{!product.prod.Description}" />
                 <apex:column value="{!product.prod.Family}" />           
           </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>

 Please help