Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a schedulable class in my managed application. (I feel that some of the code I am about to discuss is not good code so in my defence I want to clarify that I did not write this code - I just support it)

In my main dev org there are no issues - test classes run and no errors. But for a COMPLETELY different reason we have spawned a patch org and I am getting test class failures on this unmodified code.

Apex Test Result Detail
Time Started    1/10/2013 11:26 AM
Class   Test_ScheduleClass_DepCalculForCalPeriod
Method Name myUnitTest
Pass/Fail   Fail
Error Message   System.Exception: AcctSol:Too many script statements: 200001
Stack Trace External entry point
Class.AcctSol.ScheduleClass_DepCalculationForCalPeriod.execute: line 5, column 1
Class.AcctSol.Test_ScheduleClass_DepCalculForCalPeriod.myUnitTest: line 6, column 1

The test class is:

@isTest
private class Test_ScheduleClass_DepCalculForCalPeriod {

    static testMethod void myUnitTest() {
        ScheduleClass_DepCalculationForCalPeriod objDepForCalPeriod = new ScheduleClass_DepCalculationForCalPeriod();
        objDepForCalPeriod.execute(null);
        system.assert(true);
    }
}

The Class itself is:

global with sharing class ScheduleClass_DepCalculationForCalPeriod implements Schedulable
{
    global void execute(SchedulableContext SC)
    {
        Cls_DepCalculationForCalPeriod ObjDCP = new Cls_DepCalculationForCalPeriod();
    }
}

And at the bottom of this post is the heavy-lifting class that actually does all the work.

Now from the reading I have done I think the correct answer is this set of code should NOT have been written as only 'schedulable' but also as batchable.

My problem with that is I don't think that is very simple because batchable code has to center around a 'batchable context' query. If you look at the heavy-lifting class below this code is querying several objects and making maps and deciding what data needs to be created.

To summarize in english what this process does is it takes 'Depreciation Calculation' (DepCalc) records and 'Depreciation Journal Entries' (DJE) and potentially updates or creates new DJEs and associates various DepCalcs to them.

The logic isn't simple and if it is as all possible I DO NOT want to have to affect it in any major way if I can get away with it.

Any suggestions as to how I can easily fix this problem without having to get into a major re-write situation ???

A second and less important item is I am curious why the test fails in the Patch org and not the main Dev Org ??? I know that the patch org has many many DepCalc records in it right now and Dev has almost none in comparison but I didn't think test classes cared about what data is in the org. In a way I am happy this is failing because what I just learned from reading today makes me think this is going to one day blow up for a customer in a live situation. Better to know now than later.

Thanks in advance for any ideas you might have

public with sharing class Cls_DepCalculationForCalPeriod
{
public Cls_DepCalculationForCalPeriod()
{
    Set<String> SetDistinctCP = new Set<String>();
    Set<Id> setDJEIds = new Set<Id>();

    Map<String,AcctSol__Depreciation_Journal_Entry__c> mapCalPeriodToDJE = new Map<String,AcctSol__Depreciation_Journal_Entry__c>();
    Map<String, List<AcctSol__Depreciation_Calculation__c>> MapCPDepCalId = new Map<String, List<AcctSol__Depreciation_Calculation__c>>();
    Map<Decimal,List<AcctSol__Depreciation_Journal_Entry_Line__c>> mapDJELforIns1 = new Map<Decimal,List<AcctSol__Depreciation_Journal_Entry_Line__c>>();
    Map<Decimal,List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>> mapDJELDforIns1 = new Map<Decimal,List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>>();
    Map<Decimal,List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>> mapDJELDforInsElse1 = new Map<Decimal,List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>>();
    Map<Id,List<AcctSol__Depreciation_Journal_Entry_Line__c>> mapDLE_lstDJEL = new Map<Id,List<AcctSol__Depreciation_Journal_Entry_Line__c>>();

    List<AcctSol__Depreciation_Journal_Entry__c> lstDJE = new List<AcctSol__Depreciation_Journal_Entry__c>();
    List<AcctSol__Depreciation_Journal_Entry__c> lstDJE_forInsert1 = new List<AcctSol__Depreciation_Journal_Entry__c>();
    List<AcctSol__Depreciation_Journal_Entry_Line__c> lstDJEL_forInsert1= new List<AcctSol__Depreciation_Journal_Entry_Line__c>();
    List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c> lstDJELD_forInsert1 = new List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>();
    List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c> lstToInsertDLELDElse = new List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>();
    List<AcctSol__Depreciation_Journal_Entry_Line__c> lstDJELtoInsertElse = new List<AcctSol__Depreciation_Journal_Entry_Line__c>();
    List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c> lstDJELD_forInsert1Else = new List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>();
    List<AcctSol__Depreciation_Journal_Entry_Line__c> lstDJELforDJEs = new List<AcctSol__Depreciation_Journal_Entry_Line__c>();
    List<AcctSol__Depreciation_Calculation__c> IntialDepCallst = new List<AcctSol__Depreciation_Calculation__c>();

    Decimal identify1 = 0;
    Decimal identify2 = 0;
    Decimal identify3 = 0;
    Decimal identify4 = 0;
    Decimal identify5 = 0;

    Date today=System.today();
    String CPofCurrDate = today.year()+'-'+today.month();

    //--------------Preparing List of Dep Cal Records which are not exists in Hierarchy of DJE records----------------
    Set<ID> DepCalcIDSet = new Set<ID>();
    List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c> lstDJELD = [select id,AcctSol__Depreciation_Calculation__c 
                    from AcctSol__Depreciation_Journal_Entry_Line_Detail__c
                    where AcctSol__Depreciation_Calculation__c!=null];
    system.debug('lstDJELD Values:'+lstDJELD);
    if(lstDJELD.size()>0)
    {
        for(AcctSol__Depreciation_Journal_Entry_Line_Detail__c eachDJELD :lstDJELD )
        {
            DepCalcIDSet.add(eachDJELD.AcctSol__Depreciation_Calculation__c);
        }
    }

    if(DepCalcIDSet!=null)
    {
        IntialDepCallst = [SELECT id,AcctSol__Depr_Date__c,AcctSol__Depriciated_Expense__c,AcctSol__Fixed_Asset__r.AcctSol__Configuration__c, AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c, AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__r.createddate from AcctSol__Depreciation_Calculation__c where id NOT IN :DepCalcIDSet And AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c!=null];
    }
    system.debug('IntialDepCallst Values:'+IntialDepCallst);
    //----------------------------------------------------------------------------------------------------------------

    lstDJE = [select id,AcctSol__Calendar_Period__c,AcctSol__Interface_Status__c,AcctSol__Configuration__c from AcctSol__Depreciation_Journal_Entry__c where CreatedDate!=null And AcctSol__Interface_Status__c = 'Unposted'];        
    system.debug('IntialDepCallst Values:'+lstDJE);
    if(lstDJE.size()>0)
    {
        for(AcctSol__Depreciation_Journal_Entry__c eachDJE:lstDJE)
        {
            setDJEIds.add(eachDJE.Id);

            if(mapCalPeriodToDJE.get(eachDJE.AcctSol__Calendar_Period__c)==null)
            mapCalPeriodToDJE.put(eachDJE.AcctSol__Calendar_Period__c,eachDJE);

        }
        system.debug('setDJEIds Values:'+setDJEIds);
        system.debug('mapCalPeriodToDJE Values:'+mapCalPeriodToDJE);
    }

    if(setDJEIds!=null)
    {
        lstDJELforDJEs = [Select Id, Name, AcctSol__Amount__c, AcctSol__Depreciation_Journal_Entry__c, AcctSol__Depreciation_Schedule__c from AcctSol__Depreciation_Journal_Entry_Line__c where AcctSol__Depreciation_Journal_Entry__c IN: setDJEIds];
        system.debug('lstDJELforDJEs Values:'+lstDJELforDJEs);
    }

    if(lstDJELforDJEs.size()>0)
    {
        for(AcctSol__Depreciation_Journal_Entry_Line__c eachObjDJEL : lstDJELforDJEs)
        {
            if(mapDLE_lstDJEL.get(eachObjDJEL.AcctSol__Depreciation_Journal_Entry__c)==null)
                mapDLE_lstDJEL.put(eachObjDJEL.AcctSol__Depreciation_Journal_Entry__c, new List<AcctSol__Depreciation_Journal_Entry_Line__c>());

            mapDLE_lstDJEL.get(eachObjDJEL.AcctSol__Depreciation_Journal_Entry__c).add(eachObjDJEL);
        }
        system.debug('mapDLE_lstDJEL Values:'+mapDLE_lstDJEL);
    }



    if(IntialDepCallst.size()>0)
    {
        for(AcctSol__Depreciation_Calculation__c DepCalc : IntialDepCallst)
        {
            String Calendarperiod = DepCalc.AcctSol__Depr_Date__c.year()+'-'+DepCalc.AcctSol__Depr_Date__C.month();


            //-------------Set of Distinct Created Date of Dep Cal Records-------------------
            if(Calendarperiod!=null || Calendarperiod!='')
            {
                SetDistinctCP.add(Calendarperiod);
            }

            //-------------Map Between Dep Cal Created Date And Dep Cal Ids------------
            if(MapCPDepCalId.get(Calendarperiod)==null)
                MapCPDepCalId.put(Calendarperiod, new List<AcctSol__Depreciation_Calculation__c>());

            MapCPDepCalId.get(Calendarperiod).add(DepCalc);
        }
        system.debug('SetDistinctCP Values:'+SetDistinctCP);
        system.debug('MapCPDepCalId Values:'+MapCPDepCalId);
    }

    if(SetDistinctCP!=null)
    {
        for(String Str : SetDistinctCP)
        {
            system.debug('In the Loop');
            if(mapCalPeriodToDJE.get(Str)==null)
            {
                system.debug('No DJE exists for CP');
                Set<Id> DepScdIds = new Set<Id>();

                List<AcctSol__Depreciation_Calculation__c> lstDCalforCP =  new List<AcctSol__Depreciation_Calculation__c>();
                //List<AcctSol__Depreciation_Schedule__c> lstDSforCmp = new List<AcctSol__Depreciation_Schedule__c>();
                List<AcctSol__Depreciation_Calculation__c> lstDCalforCmp = new List<AcctSol__Depreciation_Calculation__c>();
                Map<Id, List<AcctSol__Depreciation_Calculation__c>> MapDSIdLstDCl = new Map<Id, List<AcctSol__Depreciation_Calculation__c>>();
                Map<Id, List<AcctSol__Depreciation_Calculation__c>> MapCmpIdLstDCl = new Map<Id, List<AcctSol__Depreciation_Calculation__c>>();
                Map<Id, Set<Id>> MapCIdDSId = new Map<Id, Set<Id>>();
                Set<Id> SetCompanyIDs = new Set<Id>();
                Set<Id> SetDSIDsforCmp = new Set<Id>();
                if(MapCPDepCalId.get(Str)!=null)
                {
                    lstDCalforCP = MapCPDepCalId.get(Str);
                    system.debug('lstDCalforCP Values:'+lstDCalforCP);
                }

                //-------------Map Between Dep Schedule Id And Dep Calculation List against that Id------------- 
                for(AcctSol__Depreciation_Calculation__c ObjDCal : lstDCalforCP)
                {
                    SetCompanyIDs.add(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Configuration__c);

                    if(MapCIdDSId.get(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Configuration__c)==null)
                        MapCIdDSId.put(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Configuration__c, new Set<Id>());

                    MapCIdDSId.get(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Configuration__c).add(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c);

                    if(MapCmpIdLstDCl.get(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Configuration__c)==null)
                        MapCmpIdLstDCl.put(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Configuration__c, new List<AcctSol__Depreciation_Calculation__c>());

                    MapCmpIdLstDCl.get(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Configuration__c).add(ObjDCal);

                    if(MapDSIdLstDCl.get(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c)==null)
                        MapDSIdLstDCl.put(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c, new List<AcctSol__Depreciation_Calculation__c>());

                    MapDSIdLstDCl.get(ObjDCal.AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c).add(ObjDCal);
                }
                system.debug('MapDSIdLstDCl Values:'+MapDSIdLstDCl);

                for(Id CIds : SetCompanyIDs)
                {
                    AcctSol__Depreciation_Journal_Entry__c objDJE = new AcctSol__Depreciation_Journal_Entry__c();
                    objDJE.AcctSol__Calendar_Period__c = CPofCurrDate;
                    objDJE.AcctSol__Interface_Status__c = 'Unposted';
                    objDJE.AcctSol__Configuration__c = CIds;
                    objDJE.AcctSol__IdentifierDJE__c = identify1;
                    lstDJE_forInsert1.add(objDJE);

                    if(MapCIdDSId!=null && MapCIdDSId.get(CIds)!=null)
                    {
                        SetDSIDsforCmp = MapCIdDSId.get(CIds);
                    }

                    if(MapCmpIdLstDCl!=null && MapCmpIdLstDCl.get(CIds)!=null)
                    {
                        lstDCalforCmp = MapCmpIdLstDCl.get(CIds);
                    }

                    if(SetDSIDsforCmp!=null)
                    {
                        for(Id eachDS : SetDSIDsforCmp)
                        {
                            system.debug('No of times in to the Loop');
                            AcctSol__Depreciation_Journal_Entry_Line__c objDJEL = new AcctSol__Depreciation_Journal_Entry_Line__c ();
                            objDJEL.AcctSol__Depreciation_Schedule__c = eachDS;
                            objDJEL.AcctSol__IdentifierDJEL__c = identify2;

                            if(mapDJELforIns1.get(identify1)==null)
                                mapDJELforIns1.put(identify1,new List<AcctSol__Depreciation_Journal_Entry_Line__c>());

                            mapDJELforIns1.get(identify1).add(objDJEL);

                            if(MapDSIdLstDCl.get(eachDS)!=null)
                            {
                                List<AcctSol__Depreciation_Calculation__c> lstDClforDS = MapDSIdLstDCl.get(eachDS);
                                system.debug('lstDClforDS Values:'+lstDClforDS);

                                if(lstDCalforCmp.size()>0)
                                {
                                    for(AcctSol__Depreciation_Calculation__c eachDEC : lstDClforDS)
                                    {
                                        for(AcctSol__Depreciation_Calculation__c eachCmpDC : lstDCalforCmp)
                                        {
                                            if(eachDEC.Id == eachCmpDC.Id)
                                            {
                                                AcctSol__Depreciation_Journal_Entry_Line_Detail__c objDJELD = new AcctSol__Depreciation_Journal_Entry_Line_Detail__c();
                                                objDJELD.AcctSol__Depreciation_Calculation__c = eachDEC.Id;

                                                if(mapDJELDforIns1.get(identify2)==null)
                                                    mapDJELDforIns1.put(identify2,new List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>());

                                                mapDJELDforIns1.get(identify2).add(objDJELD);
                                            }
                                        }
                                    }
                                }
                                identify2 = identify2+1;
                            }
                        }
                       identify1 = identify1+1; 
                    }
                }
            }
            else
            {
                AcctSol__Depreciation_Journal_Entry__c objDJE = mapCalPeriodToDJE.get(Str);
                Id CmpId = objDJE.AcctSol__Configuration__c;
                system.debug('objDJE Values:'+objDJE);
                List<AcctSol__Depreciation_Journal_Entry_Line__c> lstDJELforobjDJE = new List<AcctSol__Depreciation_Journal_Entry_Line__c>();
                List<AcctSol__Depreciation_Calculation__c> lstDCalforCPElse =  new List<AcctSol__Depreciation_Calculation__c>();
                Map<Id, List<AcctSol__Depreciation_Calculation__c>> MapDSIdLstDClElse = new Map<Id, List<AcctSol__Depreciation_Calculation__c>>();
                Set<Id> DepScdIdsElse = new Set<Id>();
                boolean ischeck;
                if(objDJE!=null)
                {
                    lstDJELforobjDJE = mapDLE_lstDJEL.get(objDJE.Id);
                }

                if(MapCPDepCalId.get(Str)!=null)
                {
                    lstDCalforCPElse = MapCPDepCalId.get(Str);
                }


                //-------------Map Between Dep Schedule Id And Dep Calculation List against that Id------------- 
                for(AcctSol__Depreciation_Calculation__c ObjDCalElse : lstDCalforCPElse)
                {
                    if(ObjDCalElse.AcctSol__Fixed_Asset__r.AcctSol__Configuration__c == CmpId)
                    {
                        if(MapDSIdLstDClElse.get(ObjDCalElse.AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c)==null)
                            MapDSIdLstDClElse.put(ObjDCalElse.AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c, new List<AcctSol__Depreciation_Calculation__c>());

                        MapDSIdLstDClElse.get(ObjDCalElse.AcctSol__Fixed_Asset__r.AcctSol__Depreciation_Schedule__c).add(ObjDCalElse);
                    }
                }

                if(MapDSIdLstDClElse!=null)
                {  
                    for(Id eachDepScdIDselse : MapDSIdLstDClElse.keyset())
                    {
                        ischeck = false;
                        if(lstDJELforobjDJE!=null && lstDJELforobjDJE.size()>0)
                        {
                            for(AcctSol__Depreciation_Journal_Entry_Line__c eachObjDJEL_els : lstDJELforobjDJE)
                            {
                                if(eachDepScdIDselse == eachObjDJEL_els.AcctSol__Depreciation_Schedule__c)
                                {
                                    if(MapDSIdLstDClElse.get(eachDepScdIDselse)!=null)
                                    {
                                        List<AcctSol__Depreciation_Calculation__c> lstDepCalElse = MapDSIdLstDClElse.get(eachDepScdIDselse);

                                        for(AcctSol__Depreciation_Calculation__c eachObjDepCElse : lstDepCalElse)
                                        {
                                            AcctSol__Depreciation_Journal_Entry_Line_Detail__c objDJELDElse = new AcctSol__Depreciation_Journal_Entry_Line_Detail__c();
                                            objDJELDElse.AcctSol__Depreciation_Calculation__c = eachObjDepCElse.Id;
                                            objDJELDElse.AcctSol__Depreciation_Journal_Entry_Line__c = eachObjDJEL_els.Id;
                                            lstToInsertDLELDElse.add(objDJELDElse); 
                                            ischeck = true;
                                            //break;
                                        }
                                    }
                                }
                            }
                        }

                        if(ischeck == false)
                        {
                            AcctSol__Depreciation_Journal_Entry_Line__c objDJELElse = new AcctSol__Depreciation_Journal_Entry_Line__c ();
                            objDJELElse.AcctSol__Depreciation_Schedule__c = eachDepScdIDselse;
                            objDJELElse.AcctSol__Depreciation_Journal_Entry__c = objDJE.Id;
                            objDJELElse.AcctSol__IdentifierDJEL__c = identify3;
                            lstDJELtoInsertElse.add(objDJELElse);

                            List<AcctSol__Depreciation_Calculation__c> lstDepCalElse1 = new List<AcctSol__Depreciation_Calculation__c>();

                            if(MapDSIdLstDClElse.get(eachDepScdIDselse)!=null)
                            {
                                lstDepCalElse1 = MapDSIdLstDClElse.get(eachDepScdIDselse);

                                if(lstDepCalElse1.size()>0)
                                {
                                    for(AcctSol__Depreciation_Calculation__c eachObjDCElse1 : lstDepCalElse1)
                                    {
                                        AcctSol__Depreciation_Journal_Entry_Line_Detail__c objDJELDElse1 = new AcctSol__Depreciation_Journal_Entry_Line_Detail__c();
                                        objDJELDElse1.AcctSol__Depreciation_Calculation__c = eachObjDCElse1.Id;

                                        if(mapDJELDforInsElse1.get(identify3)==null)
                                            mapDJELDforInsElse1.put(identify3,new List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c>());

                                        mapDJELDforInsElse1.get(identify3).add(objDJELDElse1);
                                    }
                                }
                            }
                            identify3 = identify3+1;
                        }
                    }
                }
            }
        }
    }

    //-------------Record Insertion for DJE Not Exists-----------------------

      if(lstDJE_forInsert1.size()>0)
      {
          insert lstDJE_forInsert1;
      }

      lstDJE_forInsert1 = [Select Id, Name, AcctSol__IdentifierDJE__c from AcctSol__Depreciation_Journal_Entry__c where Id IN:lstDJE_forInsert1];

      for(AcctSol__Depreciation_Journal_Entry__c eachobjDJEforIns : lstDJE_forInsert1)
      {
          if(mapDJELforIns1.get(eachobjDJEforIns.AcctSol__IdentifierDJE__c)!=null)
          {
              List<AcctSol__Depreciation_Journal_Entry_Line__c> lstDJELIns = mapDJELforIns1.get(eachobjDJEforIns.AcctSol__IdentifierDJE__c);

              for(AcctSol__Depreciation_Journal_Entry_Line__c ObjDJELIns : lstDJELIns)
              {
                  ObjDJELIns.AcctSol__Depreciation_Journal_Entry__c = eachobjDJEforIns.Id;
                  lstDJEL_forInsert1.add(ObjDJELIns);
              }
          }
      }

      if(lstDJEL_forInsert1.size()>0)
      {
          insert lstDJEL_forInsert1;
      }

      lstDJEL_forInsert1 = [Select Id, Name, AcctSol__IdentifierDJEL__c from AcctSol__Depreciation_Journal_Entry_Line__c where Id IN:lstDJEL_forInsert1];

      for(AcctSol__Depreciation_Journal_Entry_Line__c eachobjDJELIns : lstDJEL_forInsert1)
      {
          if(mapDJELDforIns1.get(eachobjDJELIns.AcctSol__IdentifierDJEL__c)!=null)
          {
              List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c> lstDJELDIns = mapDJELDforIns1.get(eachobjDJELIns.AcctSol__IdentifierDJEL__c);

              for(AcctSol__Depreciation_Journal_Entry_Line_Detail__c ObjDJELDIns : lstDJELDIns)
              {
                  ObjDJELDIns.AcctSol__Depreciation_Journal_Entry_Line__c = eachobjDJELIns.Id;
                  lstDJELD_forInsert1.add(ObjDJELDIns);
              }
          }
      }

      if(lstDJELD_forInsert1.size()>0)
      {
          insert lstDJELD_forInsert1;
      }

      //------------------Record Insertion For DJE Exist--------------------------

      if(lstToInsertDLELDElse.size()>0)
      {
          insert lstToInsertDLELDElse;
      }


      if(lstDJELtoInsertElse.size()>0)
      {
          insert lstDJELtoInsertElse;
      }

      lstDJELtoInsertElse = [Select Id, Name, AcctSol__IdentifierDJEL__c from AcctSol__Depreciation_Journal_Entry_Line__c where Id IN:lstDJELtoInsertElse];

      for(AcctSol__Depreciation_Journal_Entry_Line__c eachobjDJELInsElse : lstDJELtoInsertElse)
      {
          if(mapDJELDforInsElse1.get(eachobjDJELInsElse.AcctSol__IdentifierDJEL__c)!=null)
          {
              List<AcctSol__Depreciation_Journal_Entry_Line_Detail__c> lstDJELDInsElse = mapDJELDforInsElse1.get(eachobjDJELInsElse.AcctSol__IdentifierDJEL__c);

              for(AcctSol__Depreciation_Journal_Entry_Line_Detail__c ObjDJELDInsElse : lstDJELDInsElse)
              {
                  ObjDJELDInsElse.AcctSol__Depreciation_Journal_Entry_Line__c = eachobjDJELInsElse.Id;
                  lstDJELD_forInsert1Else.add(ObjDJELDInsElse);
              }
          }
      }

      if(lstDJELD_forInsert1Else.size()>0)
      {
          insert lstDJELD_forInsert1Else;
      }
  }
}
share|improve this question
    
What API version is the class using? Is it the same in both orgS? Could be that if you're on an older one (I forget which exactly) that it's picking up data from DB and that's making a difference. –  LaceySnr Jan 11 '13 at 3:40

1 Answer 1

up vote 4 down vote accepted

This is happening because the loops are iterating more data than can be processed before reaching the script statement limit. If you iterate 100 records in a for loop and within each iteration you execute 400 lines of code, you've just consumed 40,000 script statements.

Presumably your patch org is full of lots of data from production and is thus returning large datasets when they are retrieved using an unbounded query, where your other sandboxes are relatively empty. For example: where AcctSol__Depreciation_Calculation__c != null is a risky where clause which could return a very large amount of data from the system.

If you want to isolate your test from the org data and are not utilizing any of the objects noted in this SalesForce article about isolation of test data, you can change the metadata version of the class to 24 or higher and add the annotation of seeAllData=false to the test class. At that point it will behave as you expected, likely passing the test but you've definetly uncovered something that will fail sooner or later unless the underlying code is addressed.

You can probably easily determine the cause through debug statements sprinkled in this class to tell you the number of rows being processed in each iteration and make any necessary corrections or limits to the queries within.

One starting point could be to look at how many rows the debug log indicates are returned by each of the queries in the Cls_DepCalculationForCalPeriod class. Running through your code and some rough multiplication can point you toward the loop which is causing you trouble here.

Some SalesForce 'Best Practices' which would be useful to you in this situation are found here, #6 and #7. #6 might allow you to more efficiently iterate your data and #7 would allow you to prevent further processing if you are approaching a limit rather than crashing into it head on and throwing an exception.

Apex Code Best Practices

share|improve this answer
    
I have solved the problem. We decided that the current processing - which would take ALL unprocessed Dep Calc records and try to work with them isn't either required or sensible. I changed process so that only 1 calendar month is processed at a time - that ended up being a fairly minor change. I estimate that before my changes I was attempting to run 1100 records through a loop containing approximately 110 lines of code (121,000) down to 12,100. Thanks for your assistance! –  AngiB Jan 11 '13 at 14:52
    
In regards to my test classes failing - I am actually happy that they did - otherwise because I had no clue this limit existed I might have learned of it only when a customer encountered the problem. That would of course make the situation a dire emergency... at least we are aware now. –  AngiB Jan 11 '13 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.