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
EQ AdminEQ Admin 

I have written a trigger on account which will update all the chil account locations Address if shipping and billing addres change on account. It work well for individual record but during bulk update from Data loader it throws an error - CPU timeout.

I have not used any For inside For still getting this error. 
Any help will be appreciated.
EQ AdminEQ Admin
Here is the code: 

public without sharing class APTS_AccountActions 
{

    
    public static void updateAccountLocationAdress(List<Account> newAccount)
    {
          List<Apttus_Config2__AccountLocation__c> accToInsert = new List<Apttus_Config2__AccountLocation__c>();
          List<Apttus_Config2__AccountLocation__c> accLocShipToInsert = new List<Apttus_Config2__AccountLocation__c>();
          Map<Id,List<Apttus_Config2__AccountLocation__c>> accIdWithOpplIstMap=new Map<Id,List<Apttus_Config2__AccountLocation__c>>();
          Map<Id,List<Apttus_Config2__AccountLocation__c>> accIdWithOppBillTolIstMap=new Map<Id,List<Apttus_Config2__AccountLocation__c>>(); 
          Map<Id,List<Apttus_Config2__AccountLocation__c>> accIdWithOppShipTolIstMap=new Map<Id,List<Apttus_Config2__AccountLocation__c>>(); 
      for(Apttus_Config2__AccountLocation__c acc:[SELECT Id,
                                                             Apttus_Config2__Type__c,
                                                             Apttus_Config2__AccountId__c,
                                                              
                                                             Apttus_Config2__Street__c ,
                                                             EQ_Street2__c,EQ_Street3__c,
                                                             Apttus_Config2__City__c,EQ_Country__c,
                                                             EQ_ParentAccountLocation__c,
                                                             Apttus_Config2__County__c,
                                                             EQ_State__c,EQ_SourceSystem__c,
                                                             Apttus_Config2__State__c,
                                                             Apttus_Config2__PostalCode__c  
                                                              FROM Apttus_Config2__AccountLocation__c 
                                                             WHERE Apttus_Config2__AccountId__c IN: newAccount])
    {
          
      accIdWithOpplIstMap.put(acc.Apttus_Config2__AccountId__c,new List<Apttus_Config2__AccountLocation__c>{acc});
             if((acc.Apttus_Config2__Type__c=='Bill To'))
                {
                  accIdWithOppBillTolIstMap.put(acc.Apttus_Config2__AccountId__c,new List<Apttus_Config2__AccountLocation__c>{acc});
                }
             else if((acc.Apttus_Config2__Type__c=='Ship To'))
               {
            accIdWithOppShipTolIstMap.put(acc.Apttus_Config2__AccountId__c,new List<Apttus_Config2__AccountLocation__c>{acc});
               }
          
    }
        if(accIdWithOpplIstMap.size()>0)
        {
            List<Apttus_Config2__AccountLocation__c> finalDML = new List<Apttus_Config2__AccountLocation__c>();
            List<Apttus_Config2__AccountLocation__c> finalShipToDML = new List<Apttus_Config2__AccountLocation__c>();
            for(Apttus_Config2__AccountLocation__c accLoc : [ SELECT Id,
                                                                 Apttus_Config2__AccountId__c,
                                                                 Apttus_Config2__Type__c,
                                                                 EQ_ParentAccountLocation__r.EQ_SourceSystem__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingAddressName__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingCity__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingStreet1__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingStreet2__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingStreet3__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingPostalCode__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingCounty__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingCountry__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingState_nonUS__c,
                                                                 Apttus_Config2__AccountId__r.EQ_BillingState__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingAddressName__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingCity__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingStreet1__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingStreet2__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingStreet3__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingPostalCode__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingCounty__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingCountry__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingState_nonUS__c,
                                                                 Apttus_Config2__AccountId__r.EQ_ShippingState__c,
                                                                 Apttus_Config2__Street__c ,
                                                                 EQ_Street2__c,EQ_Street3__c,
                                                                 EQ_ParentAccountLocation__c,
                                                                 Apttus_Config2__City__c,EQ_Country__c,
                                                                 Apttus_Config2__County__c,
                                                                 EQ_State__c,
                                                                 Apttus_Config2__State__c,EQ_SourceSystem__c,
                                                                 Apttus_Config2__PostalCode__c 
                                                                 FROM Apttus_Config2__AccountLocation__c
                                                                WHERE (Apttus_Config2__AccountId__c IN: accIdWithOpplIstMap.keyset()) ])
  {
                             
            if(((accLoc.Apttus_Config2__Type__c=='Bill To')&&(accLoc.EQ_SourceSystem__c=='CRM'))||
            ((accLoc.Apttus_Config2__Type__c=='Bill To')&&(accLoc.EQ_ParentAccountLocation__c!=null)
             &&(accLoc.EQ_ParentAccountLocation__r.EQ_SourceSystem__c=='CRM')))
                   {
                       // accLoc.Name=accLoc.Apttus_Config2__AccountId__r.EQ_BillingAddressName__c;
                        accLoc.Apttus_Config2__City__c = accLoc.Apttus_Config2__AccountId__r.EQ_BillingCity__c;
                        accLoc.Apttus_Config2__Street__c = accLoc.Apttus_Config2__AccountId__r.EQ_BillingStreet1__c;
                        accLoc.EQ_Street2__c=accLoc.Apttus_Config2__AccountId__r.EQ_BillingStreet2__c;
                        accLoc.EQ_Street3__c=accLoc.Apttus_Config2__AccountId__r.EQ_BillingStreet3__c;
                        accLoc.Apttus_Config2__PostalCode__c = accLoc.Apttus_Config2__AccountId__r.EQ_BillingPostalCode__c;
                        accLoc.Apttus_Config2__County__c = accLoc.Apttus_Config2__AccountId__r.EQ_BillingCounty__c;
                        accLoc.EQ_Country__c = accLoc.Apttus_Config2__AccountId__r.EQ_BillingCountry__c;
                        accLoc.Apttus_Config2__State__c = accLoc.Apttus_Config2__AccountId__r.EQ_BillingState_nonUS__c;
                        accLoc.EQ_State__c = accLoc.Apttus_Config2__AccountId__r.EQ_BillingState__c;
                        finalDML.add(accLoc);
                    }  
     
          if(((accLoc.Apttus_Config2__Type__c=='Ship To')&&(accLoc.EQ_SourceSystem__c=='CRM'))||
            ((accLoc.Apttus_Config2__Type__c=='Ship To')&&(accLoc.EQ_ParentAccountLocation__c!=null)
             &&(accLoc.EQ_ParentAccountLocation__r.EQ_SourceSystem__c=='CRM')))
                   {
                            ///accLoc.Name=accLoc.Apttus_Config2__AccountId__r.EQ_ShippingAddressName__c;
                            accLoc.Apttus_Config2__City__c = accLoc.Apttus_Config2__AccountId__r.EQ_ShippingCity__c;
                            accLoc.Apttus_Config2__Street__c = accLoc.Apttus_Config2__AccountId__r.EQ_ShippingStreet1__c;
                            accLoc.EQ_Street2__c=accLoc.Apttus_Config2__AccountId__r.EQ_ShippingStreet2__c;
                            accLoc.EQ_Street3__c=accLoc.Apttus_Config2__AccountId__r.EQ_ShippingStreet3__c;
                            accLoc.Apttus_Config2__PostalCode__c = accLoc.Apttus_Config2__AccountId__r.EQ_ShippingPostalCode__c;
                            accLoc.Apttus_Config2__County__c = accLoc.Apttus_Config2__AccountId__r.EQ_ShippingCounty__c;
                            accLoc.EQ_Country__c = accLoc.Apttus_Config2__AccountId__r.EQ_ShippingCountry__c;
                            accLoc.Apttus_Config2__State__c = accLoc.Apttus_Config2__AccountId__r.EQ_ShippingState_nonUS__c;
                            accLoc.EQ_State__c = accLoc.Apttus_Config2__AccountId__r.EQ_ShippingState__c;
                            finalShipToDML.add(accLoc);
                        }
      
  }                                                       
      update finalDML;
       update  finalShipToDML;
       
Dev-FoxDev-Fox
This will not work this way, but if you still want to make it work, set the batch size to 1 in data loader settings before uploading the data.
EQ AdminEQ Admin
Ohh...Thanks for your response. Could you please let me know what could be done to improve this code so that it work on bulk update