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
Ramesh RageRamesh Rage 

Getting Apex CPU time limit exceeded error while looping

Hi ,

we are Getting Apex CPU time limit exceeded error while looping thte large no of records.
According to our requirement we receive large file from webmethods(the file may contains 50,000 records) then need to inset arround all records through integration code.

while looping all the records getting below error.

12:43:33.539 (20539843589)|EXCEPTION_THROWN|[509]|System.LimitException: Apex CPU time limit exceeded
12:43:33.539 (20539876910)|SYSTEM_MODE_EXIT|false
12:43:33.539 (20539934252)|FATAL_ERROR|System.LimitException: Apex CPU time limit exceeded

Class.Arqiva_IntegrationUpdates3up.cartonUnitsToInsert: line 509, column 1
Class.Arqiva_IntegrationUpdates3up.ASNAckUpdate: line 303, column 1
AnonymousBlock: line 145, column 1
AnonymousBlock: line 145, column 1
12:43:33.808 (20540012301)|CUMULATIVE_LIMIT_USAGE
12:43:33.808|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 5 out of 100
  Number of query rows: 8 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 3 out of 150
  Number of DML rows: 391 out of 10000
  Maximum CPU time: 15042 out of 10000 ******* CLOSE TO LIMIT

anyone please help on thisissue.

Regards,
Naveen

 
MithunPMithunP

Hi Naveen,

Recently salesforce introduced new governer limit called "CPU time on the Salesforce servers", for Synchronous 10,000 milliseconds and Asynchronous 60,000 milliseconds.

If your lists having large data then don't use nested FOR loops (for loop within for loop) because it will easily reach CPU time, instead use MAP's like below.

Using Nested For loop
 
List<Task> tasks = [SELECT Id, WhoId FROM Task];
List<Contact> contacts = [SELECT Id FROM Contact];

for (Task t : tasks){
    for(Contact c : contacts){
        if (t.WhoId == c.Id){
            //some logic here   
        } 
    }
}


Using Map's
List<Task> tasks = [SELECT Id ... FROM Task];
Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id ... FROM Contact]);

for (Task t : tasks){
    Contact c = contacts.get(t.WhoId);
    if(c != null) {
        //some logic here   
    }
}



Best Regards,
Mithun.
Ramesh RageRamesh Rage
Hi Mithun,

Thanks for the update.
Here in my reqirement the scenario is diffrent, need to insert the data we don't have any ids.

Please check the below code

global class carton
{
             webservice List<Unit> Units;
             webservice String QuantityofUnitsperCarton;
             webservice String cartonidentifier;

}

global class Unit
{
             webservice string PartCHvariantname;
             webservice string CHFid;
             webservice string GPFid;
             webservice string Flexnetid;
             webservice string ZigbeeMac;
           //webservice string DesOfCHvarient;
             webservice string HardWareVersion;
             webservice string SoftwareVersion;
             webservice string DeviceConfigIdentifier; 
             webservice string Manufacturer;
             webservice string DateofManufacture;
             Webservice string CountryofManufacture;
             Webservice string BatchNumber;
             Webservice String ReconditionedStatus; 

}


 global static list<Arq_Carton_Unit__c > cartonUnitsToInsert(list<carton> cartons,map<string,Id>Carton_ID_Cartonidentifier, map<String,String> Unit_name,map<String,String> unit_shotdesc){
                 
           integer i=0;
        
          Transient  List<Arq_Carton_Unit__c> cartonUnitsToInsert = new List<Arq_Carton_Unit__c>(); 
            Transient  list<Arq_Carton_Unit__c> cartonToInsertList1 = new list<Arq_Carton_Unit__c>() ;
           Transient  list<Arq_Carton_Unit__c> cartonToInsertList2 = new list<Arq_Carton_Unit__c>();
           Transient  list<Arq_Carton_Unit__c> cartonToInsertList3  = new list<Arq_Carton_Unit__c>();
           Transient  list<Arq_Carton_Unit__c> cartonToInsertList4  = new list<Arq_Carton_Unit__c>();
           Transient  list<Arq_Carton_Unit__c> cartonToInsertList5  = new list<Arq_Carton_Unit__c>();
            System.debug('XXX: cartons to insert: ' + cartonUnitsToInsert );
                      
            //below loop is for final mapping for unit(creation of units and map to the appropriate fields)            
            for(carton c : cartons){
            system.debug('Cartons'+c);
               if(Carton_ID_Cartonidentifier.containsKey(c.cartonidentifier))
                 {
                 Id OSPIId = Carton_ID_Cartonidentifier.get(c.cartonidentifier);                   
                    for (Unit o: c.Units)
                        {
                         system.debug('cartons'+o);
                         Arq_Carton_Unit__c OSCURec = new Arq_Carton_Unit__c(); 
                         OSCURec.Name =   o.PartCHvariantname;
                         OSCURec.Arq_Description_of_CH_variant__c = Unit_name.get(o.PartCHvariantname);
                         OSCURec.Arq_Description_of_CH_variant__c = unit_shotdesc.get(o.PartCHvariantname);
                          OSCURec.Arq_CHF_ID__c= o.CHFid;
                         OSCURec.Arq_GPF_ID__c= o.GPFid;
                         OSCURec.Arq_FlexNet_Id__c= o.Flexnetid;
                         OSCURec.Arq_ZigBee_MAC__c= o.ZigbeeMac;                         
                         OSCURec.Arq_Hardware_Version__c= o.HardWareVersion;
                         OSCURec.Arq_Firmware_Version__c= o.SoftwareVersion;
                         OSCURec.Arq_Device_Configuration_Identifier__c= o.DeviceConfigIdentifier;
                         OSCURec.Arq_Manufacturer__c= o.Manufacturer;
                         Date DateofManufacture = date.valueOf(o.DateofManufacture);
                         OSCURec.Arq_Manufacture_Date__c= DateofManufacture ;
                         OSCURec.Arq_Country_of_Manufacture__c= o.CountryofManufacture;
                         OSCURec.Arq_Btach_No__c= o.Batchnumber;
                         OSCURec.Arq_Reconditioned_Status__c=o.ReconditionedStatus;
                         OSCURec.Arq_Shipment_Carton__c = OSPIId;
                      
                        // cartonUnitsToInsert.add(OSCURec);
                         if(i <=9499 )
                           {
                                       cartonToInsertList1.add(OSCURec);
                                       system.debug('!!!!!!!!!!UnitList1'+ cartonToInsertList1);
                                      
                           }
                           if(i>9499 && i < =19498 )
                           {
                                       cartonToInsertList2.add(OSCURec);
                                        system.debug('!!!!!!!!!!UnitList2'+ cartonToInsertList2);
                                      
                           }
                               if(i>19498 && i < =29497 )
                           {
                                       cartonToInsertList3.add(OSCURec);
                                        system.debug('!!!!!!!!!!UnitList3'+ cartonToInsertList3);
                                       
                           }
                            if( i>29497 && i < =39496 )
                           {
                                       cartonToInsertList4.add(OSCURec);
                                        system.debug('!!!!!!!!!!UnitList4'+ cartonToInsertList4);
                                       
                           }
                               if(i>39496 && i < =49495 )
                           {
                                       cartonToInsertList5.add(OSCURec);
                                        system.debug('!!!!!!!!!!UnitList5'+ cartonToInsertList5);
                                       
                           }
                           
                           i++;                   
                        }
                   }
                
             }//end of Loop
                     
      
      return null;
          
}

Regards,
Naveen