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
kmorf_entransformkmorf_entransform 

too many script statements:20001

Im getting an error that says too many script statements:20001. Does anyone know how to overcome this. here is the code snipet where im getting this error

public PageReference Upload()
    {
    	
    	
        if(IsTest == null)
            IsTest = False;            
        if(!IsTest && (contentFile.Tostring() == null || contentFile.Tostring() == ''))
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.ERROR,'Please select the data csv file to insert!');
                ApexPages.addMessage(msg);
                return null;
        }
        
        
        
        // Get all the fields of object selected in the form of map
        String errorOccuredRow = ''; 
        String errorOccuredCol = '';         
        String sMsg = '';
        Map<String, Schema.SObjectField> objectFieldMap = null;
        if(selObject == 'Asset')
        {
            objectFieldMap = Schema.getGlobalDescribe().get('sevro_Terra_Asset__c').getDescribe().fields.getMap();
        }
        else
            objectFieldMap = Schema.getGlobalDescribe().get(selObject).getDescribe().fields.getMap();
        
        List<String> lFileRows = new List<String>();
        try 
        {
            if(IsTest == null)
                IsTest = False;
            if(!IsTest)
            {
                //It Splits all the rows of file in array
                lFileRows = contentFile.toString().split('\n');
                
            }
            else
            {
                lFileRows = sTest.split('\n');
            }
            // First column of file contains name of columns
            String[] columnList = lFileRows[0].split(',');
            // If selected object is Account
            if(selObject == 'Account')
              {
                List<FieldMappingAccount__c> lFieldMapping = new List<FieldMappingAccount__c>();
                Map<String,String> mFieldInfo = null;
                lFieldMapping = FieldMappingAccount__c.getAll().values();
                mFieldInfo = new Map<String,String>();
                for(FieldMappingAccount__c obj:lFieldMapping)
                {
                    mFieldInfo.put(String.valueOf(obj.Name),String.valueOf(obj.SFDC_Field_API_Name__c));
                }
                  
                  //Iterate each file record except Header row which have column name
                  for(integer i = 1; i < lFileRows.size(); i++)
                  {
                   // Array stores values of individual row record
                   String[] arrRowValues = lFileRows[i].split(',');
                   System.debug('Value of rows are' + arrRowValues);
        
                   // Account Object
                   Account Acc = new Account();
                   
                   // Iterate individual row array values
                   for(integer j = 0; j < arrRowValues.size(); j++)
                   {
                          
                      try
                      {
                          // On the basis of Column name from column Array( columnList[j]) 
                          String sField = mFieldInfo.get(columnList[j]);
                          Acc.put(sField,arrRowValues[j]);                
                      }
                      catch(Exception e)
                      {
                        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Invalid Column: '+ columnList[j]));
                        return null;  
                      }
                   }
                    accList.add(Acc);
                   System.debug('Value of account list is' + accList);
                  }
                  if(accList.size() > 0)
                    insert accList;
                  sMsg = 'Total Accounts Uploaded:----->' + accList.size();
              }
              else if(selObject == 'Contact')
              {
                List<FieldMappingContact__c> lFieldMapping = new List<FieldMappingContact__c>();
                Map<String,String> mFieldInfo = null;
                lFieldMapping = FieldMappingContact__c.getAll().values();
                mFieldInfo = new Map<String,String>();
                for(FieldMappingContact__c obj:lFieldMapping)
                {
                    mFieldInfo.put(String.valueOf(obj.Name),String.valueOf(obj.SFDC_Field_API_Name__c));
                }
                  
                  //Iterate each file record except Header row which have column name
                  for(integer i=1; i < lFileRows.size(); i++)
                  {
                   // Array stores values of individual row record
                   String[] arrRowValues = lFileRows[i].split(',');
                   System.debug('Value of rows are' + arrRowValues);
        
                   // Account Object
                   Contact Cont = new Contact();
                   
                   // Iterate individual row array values
                   for(integer j = 0; j < arrRowValues.size(); j++)
                   {
                      try
                      {
                          // On the basis of Column name from column Array( columnList[j]) 
                          String sField = mFieldInfo.get(columnList[j]);
                          Cont.put(sField,arrRowValues[j]);
                      }
                      catch(Exception e)
                      {
                        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Invalid Column: '+ columnList[j]));
                        return null;  
                      }
                   }
                   
                   lCont.add(Cont);
                  }
                  if(lCont.size() > 0)
                    insert lCont;
                  sMsg = 'Total Contacts Uploaded:----->' + lCont.size();
              }
            else if(selObject == 'Asset')
            {   
                   
                List<FieldMapping__c> lFieldMapping = new List<FieldMapping__c>();
                Map<String,String> mFieldInfo = null;
                lFieldMapping = FieldMapping__c.getAll().values();
                mFieldInfo = new Map<String,String>();
                for(FieldMapping__c obj:lFieldMapping)
                {
                    mFieldInfo.put(String.valueOf(obj.Name),String.valueOf(obj.SFDC_Field_API_Name__c));                    
                }
                
              for( Integer i = 1 ; i < lFileRows.size() ; i++ )
              {
              	
                // Array stores values of individual row record
                String[] arrRowValues = lFileRows[i].split(',');
                CSVAssetModel assetModel = new CSVAssetModel();
                String rowType = null;
                assetModel.sRowType = '';
                
                /*
                if(arrRowValues.get(0) != null)
                    rowType = arrRowValues.get(0).trim();
                assetModel.sRowType = rowType;
                */
                if(arrRowValues.get(0) != null)
                {
                    assetModel.sPattel_Tag = arrRowValues.get(0).trim();
                    allSerials.add(assetModel.sPattel_Tag);
                }
                if(arrRowValues.get(1) !=null)
                    assetModel.sCategory = arrRowValues.get(1).trim();
                if(arrRowValues.get(2) !=null)
                    assetModel.sDescription = arrRowValues.get(2).trim();
                if(arrRowValues.get(3) !=null)
                    assetModel.sManufacturer = arrRowValues.get(3).trim();
                if (arrRowValues.get(4) != null)
                    assetModel.sModel= arrRowValues.get(4).trim();
                if(arrRowValues.get(5) != null)
                    assetModel.sQty = arrRowValues.get(5).trim();
                if(arrRowValues.get(6) != null)
                    assetModel.scondition = arrRowValues.get(6).trim();
                if(arrRowValues.get(7) != null)
                    assetModel.ssellerRack = arrRowValues.get(7).trim();
                 if(arrRowValues.get(8) != null)
                    assetModel.spalletTotalWeight =  arrRowValues.get(8).trim();
                if(arrRowValues.get(9) != null)
                    assetModel.sTotal_Qty = arrRowValues.get(9).trim(); 
                   
           
                sevro_Terra_Asset__c SevroTerraAsset = new sevro_Terra_Asset__c();
               SevroTerraAsset.put('Name',assetModel.sPattel_Tag );
                //SevroTerraAsset = Schema.SobjectType.newSObject(newID);
                // Iterate individual row array values
                
                for(integer j = 2; j < columnList.size(); j++)
                {
                    system.debug('Nitin---mFieldInfo->'+ mFieldInfo);
                    system.debug('Nitin---mFieldInfo->'+ mFieldInfo.get(columnList[j]));
                    system.debug('Nitin---columnList[j]->'+columnList[j]);
                    
                    // On the basis of Column name from column Array() 
                    String sField = String.valueOf(mFieldInfo.get(columnList[j]));
                    
                    system.debug('Nitin---sField->'+ sField);                   
                    
                    if(sField != null && sField.length() > 0 && arrRowValues[j] != null && String.valueOf(arrRowValues[j]).trim().length() > 0)
                    {
                        
                        String SFDataType = String.valueOf(objectFieldMap.get(sField).getDescribe().getType());
                        system.debug('Nitin---SFDataType->'+ SFDataType);      
                        
                                      
                        if(SFDataType == 'Schema.DisplayType.DATE')
                            SevroTerraAsset.put(sField,ConvertStrToDate(arrRowValues[j]));
                        else if(SFDataType == 'Schema.DisplayType.DATETIME')
                            SevroTerraAsset.put(sField,ConvertStrToDateTime(arrRowValues[j]));
                        else if(SFDataType == 'Schema.DisplayType.Boolean')
                            SevroTerraAsset.put(sField,Boolean.valueOf(arrRowValues[j]));
                        else if(SFDataType == 'Schema.DisplayType.Currency')
                            SevroTerraAsset.put(sField,Double.valueOf(arrRowValues[j]));
                        else if(SFDataType == 'Schema.DisplayType.Integer')
                            SevroTerraAsset.put(sField,Integer.valueOf(arrRowValues[j]));
                        else if(SFDataType == 'Schema.DisplayType.DOUBLE')
                            SevroTerraAsset.put(sField,Double.valueOf(arrRowValues[j]));
                        else if(SFDataType == 'DOUBLE')
                            SevroTerraAsset.put(sField,Double.valueOf(arrRowValues[j]));   
                        else
                            SevroTerraAsset.put(sField,String.valueOf(arrRowValues[j]));
                    }//end of if
                }//end of for
                
                
                assetModel.Sevro_Terra_Asset = SevroTerraAsset;
                csvAssetList.add(assetModel);
                
              }//end of for
            if(allSerials !=null && allSerials.size()>0)
                existingAssets = [select id, name, serialNumber__c, servo_Terra_Category__c, Description__c, Pallet_Total_Qty__c, BegInv__c from sevro_Terra_Asset__c where serialNumber__c in :allSerials];
            createAssets();
            system.debug('Nitin--serialNumberMap-> '+serialNumberMap);
            if(serialNumberMap != null && serialNumberMap.size() > 0)
                upsert serialNumberMap.values();    
            //createComponents();
            //system.debug('-----compMap-->' + assetComponentsMap);
            //if(assetComponentsMap !=null && assetComponentsMap.size()>0)
                //insert assetComponentsMap.values();         
            sMsg = 'Total Assets Uploaded:----->' + serialNumberMap.size() + '\r\t Total Components Uploaded:----->'+ assetComponentsMap.size();
            
            
        }//end of else
    }//end of try
    catch(Exception ex)
    {
       ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.ERROR,ex.getMessage() + ' , serailMapvalues' +serialNumberMap.values()+ '  ' + errorOccuredCol );
       ApexPages.addMessage(msg);
    }
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Info,sMsg));
    return null;
  }
  

 

S. KumarS. Kumar

Too many script statements could indicate you are running a lot of loops, or one loop a really long time (like maybe infinite?). You might try throwing in some system.debug statements that check how many script statements there have been so far. You would find that there's one error that kept a loop from exiting. Triggers can do this if they end up calling themselves.

 

Micky MMicky M

Hi mate i had the same problem, they only way i could find around it was to filter the data more so i was doing less loops.