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
Ti Saunders 8Ti Saunders 8 

Apex CPU Time Limit Exceeded - unexpected exception

Our org has a trigger that will update contact fields to corresponding account fields when changed. We are getting a lot of CPU time limit exceeded emails for some reason.

Here is what we have for the trigger:
AccountAfterUpdate on Account (after update) {
    
    list<Account> accounts = new list<Account>();

    // Loop through Accounts to gather modified fields
    for (Account acct : Trigger.New) {
            
        Account oldAcct = Trigger.oldMap.get(acct.Id);

/*
i_Account_Type
i_PSA/CRM
i_RMM

i_ET_Customer
i_ET_Exp_Date
i_NA_Customer
i_NA_Exp_Date
i_NA_LastRun_Date
i_NA_Purch_Date
i_NA_Report_Count
i_PW_Customer
i_PW_Exp_Date
i_SA_Customer
i_SA_Exp_Date
i_SA_LastRun_Date
i_SA_Purch_Date
i_SA_Report_Count
i_TD_Customer
i_TD_Exp_Date
i_BDR_RunCount_1y__c
i_BDR_RunCount_30d__c
i_BDR_RunCount_90d__c
i_DetectorSDS_NumDeployed__c
i_DetectorSDS_Purch_Date__c
i_EA_RunCount_1y__c
i_EA_RunCount_30d__c
i_EA_RunCount_90d__c
i_HIPAA_RunCount_1y__c
i_HIPAA_RunCount_30d__c
i_HIPAA_RunCount_90d__c
i_NA_RunCount_1y__c
i_NA_RunCount_30d__c
i_NA_RunCount_90d__c
i_PCI_RunCount_1y__c
i_PCI_RunCount_30d__c
i_PCI_RunCount_90d__c
i_Reporter_Units__c
i_SA_RunCount_1y__c
i_SA_RunCount_30d__c
i_SA_RunCount_90d__c
i_SQL_RunCount_1y__c
i_SQL_RunCount_30d__c
i_SQL_RunCount_90d__c

i_Type_of_IT__c

i_ET_Price__c
i_TD_Price__c
i_SA_Price__c
i_PW_Price__c
i_NA_Price__c

i_EA_Customer__c
i_EA_Exp_Date__c
i_EA_LastRun_Date__c
i_EA_Purch_Date__c
i_EA_Report_Count__c
i_EA_Price__c

i_ND_Module_Count__c

i_CC_Exp_Date__c
i_ET_Purch_Date__c
i_Affiliations__c


i_SQL_Customer__c
i_SQL_Exp_Date__c
i_SQL_LastRun_Date__c
i_SQL_Price__c
i_SQL_Purch_Date__c
i_SQL_Report_Count__c

i__CNCT_Customer__c
i__CNCT_Purch_Date__c
i__CNCT_Exp_Date__c
i__CNCT_Price__c

i_HIPAA_Customer__c
i_HIPAA_Exp_Date__c
i_HIPAA_Price__c
i_HIPAA_Purch_Date__c

i_INSPECTOR_Customer__c
i_INSPECTOR_Units__c

Affiliation_ASCII__c
Affiliation_CharTec__c
Affiliation_CompTIA__c
Affiliation_HTG__c
Affiliation_RR__c

Business_Type__c, FMIT_ID__c, i_HIPAA_Report_Count__c, i_HIPAA_LastRun_Date__c,
i_NDS_HC_Customer__c, i_NDS_HC_Exp_Date__c, i_NDS_HC_Purch_Date__c, i_NDS_HC_Price__c, i_NDS_HC_Report_Count__c, i_NDS_HC_LastRun_Date__c

i_PCI_Customer__c, i_PCI_Exp_Date__c, i_PCI_LastRun_Date__c, i_PCI_Price__c, i_PCI_Purch_Date__c, i_PCI_Report_Count__c,
i_MSA_Customer__c, i_MSA_Exp_Date__c, i_MSA_LastRun_Date__c, i_MSA_Price__c, i_MSA_Purch_Date__c, i_MSA_Report_Count__c

i_DETECTOR_Customer__c, i_DETECTOR_Units__c, i_REPORTER_Customer__c, i_REPORTER_Units__c, Sales_Rep__c, i_OEM_Continuum__c

i_BDR_Customer_c__c, i_BDR_Exp_Date_c__c, i_BDR_LastRun_Date_c__c, i_BDR_Price_c__c, i_BDR_Purch_Date_c__c, i_BDR_Report_Count_c__c

i_DetectorSDS_Customer__c, i_DetectorSDS_Exp_Date__c, i_DetectorSDS_LastActivation_Date__c, i_DetectorSDS_NumDeployed__c, i_DetectorSDS_Price__c, i_DetectorSDS_Purch_Date__c

*/

        if (acct.i_Account_Type__c != oldAcct.i_Account_Type__c || acct.i_PSA_CRM__c != oldAcct.i_PSA_CRM__c ||
            acct.i_RMM__c != oldAcct.i_RMM__c || acct.i_ET_Customer__c != oldAcct.i_ET_Customer__c ||
            acct.i_ET_Exp_Date__c != oldAcct.i_ET_Exp_Date__c || acct.i_NA_Customer__c != oldAcct.i_NA_Customer__c || 
            acct.i_NA_Exp_Date__c != oldAcct.i_NA_Exp_Date__c || acct.i_NA_LastRun_Date__c != oldAcct.i_NA_LastRun_Date__c || 
            acct.i_NA_Purch_Date__c != oldAcct.i_NA_Purch_Date__c || acct.i_NA_Report_Count__c != oldAcct.i_NA_Report_Count__c || 
            acct.i_PW_Customer__c != oldAcct.i_PW_Customer__c || acct.i_PW_Exp_Date__c != oldAcct.i_PW_Exp_Date__c || 
            acct.i_SA_Customer__c != oldAcct.i_SA_Customer__c || acct.i_SA_Exp_Date__c != oldAcct.i_SA_Exp_Date__c || 
            acct.i_SA_LastRun_Date__c != oldAcct.i_SA_LastRun_Date__c || acct.i_SA_Purch_Date__c != oldAcct.i_SA_Purch_Date__c || 
            acct.i_SA_Report_Count__c != oldAcct.i_SA_Report_Count__c ||  
            acct.i_Type_of_IT__c != oldAcct.i_Type_of_IT__c || acct.i_ET_Price__c != oldAcct.i_ET_Price__c ||
            acct.i_SA_Price__c != oldAcct.i_SA_Price__c || 
            acct.i_PW_Price__c != oldAcct.i_PW_Price__c || acct.i_NA_Price__c != oldAcct.i_NA_Price__c || 
            acct.i_EA_Customer__c != oldAcct.i_EA_Customer__c || acct.i_EA_Exp_Date__c != oldAcct.i_EA_Exp_Date__c ||
            acct.i_EA_LastRun_Date__c != oldAcct.i_EA_LastRun_Date__c || acct.i_EA_Purch_Date__c != oldAcct.i_EA_Purch_Date__c ||
            acct.i_EA_Report_Count__c != oldAcct.i_EA_Report_Count__c || acct.i_EA_Price__c != oldAcct.i_EA_Price__c ||
            acct.i_ND_Module_Count__c != oldAcct.i_ND_Module_Count__c || acct.i_CC_Exp_Date__c != oldAcct.i_CC_Exp_Date__c ||
            acct.i_ET_Purch_Date__c != oldAcct.i_ET_Purch_Date__c || acct.i_Affiliations__c != oldAcct.i_Affiliations__c || 
            acct.i_Refresh__c != oldAcct.i_Refresh__c || 
            acct.i_SQL_Customer__c != oldAcct.i_SQL_Customer__c || acct.i_SQL_Exp_Date__c != oldAcct.i_SQL_Exp_Date__c ||
            acct.i_SQL_LastRun_Date__c != oldAcct.i_SQL_LastRun_Date__c || acct.i_SQL_Price__c != oldAcct.i_SQL_Price__c ||
            acct.i_SQL_Purch_Date__c != oldAcct.i_SQL_Purch_Date__c || acct.i_SQL_Report_Count__c != oldAcct.i_SQL_Report_Count__c ||
            acct.i_CNCT_Customer__c != oldAcct.i_CNCT_Customer__c || acct.i_CNCT_Purch_Date__c != oldAcct.i_CNCT_Purch_Date__c ||
            acct.i_CNCT_Exp_Date__c != oldAcct.i_CNCT_Exp_Date__c || acct.i_CNCT_Price__c != oldAcct.i_CNCT_Price__c ||
            acct.i_HIPAA_Customer__c != oldAcct.i_HIPAA_Customer__c || acct.i_HIPAA_Exp_Date__c != oldAcct.i_HIPAA_Exp_Date__c ||
            acct.i_HIPAA_Price__c != oldAcct.i_HIPAA_Price__c || acct.i_HIPAA_Purch_Date__c != oldAcct.i_HIPAA_Purch_Date__c ||
            acct.i_INSPECTOR_Customer__c != oldAcct.i_INSPECTOR_Customer__c || acct.i_INSPECTOR_Units__c != oldAcct.i_INSPECTOR_Units__c ||
            acct.Affiliation_ASCII__c != oldAcct.Affiliation_ASCII__c || acct.Affiliation_CharTec__c != oldAcct.Affiliation_CharTec__c ||
            acct.Affiliation_CompTIA__c != oldAcct.Affiliation_CompTIA__c || acct.Affiliation_HTG__c != oldAcct.Affiliation_HTG__c ||
            acct.Affiliation_RR__c != oldAcct.Affiliation_RR__c ||
            acct.Business_Type__c != oldAcct.Business_Type__c || acct.FMIT_ID__c != oldAcct.FMIT_ID__c || acct.i_HIPAA_Report_Count__c != oldAcct.i_HIPAA_Report_Count__c || 
            acct.i_HIPAA_LastRun_Date__c != oldAcct.i_HIPAA_LastRun_Date__c || 
            acct.i_PCI_Customer__c != oldAcct.i_PCI_Customer__c || acct.i_PCI_Exp_Date__c != oldAcct.i_PCI_Exp_Date__c || acct.i_PCI_LastRun_Date__c != oldAcct.i_PCI_LastRun_Date__c || 
            acct.i_PCI_Price__c != oldAcct.i_PCI_Price__c || acct.i_PCI_Purch_Date__c != oldAcct.i_PCI_Purch_Date__c || acct.i_PCI_Report_Count__c != oldAcct.i_PCI_Report_Count__c ||
            acct.i_DETECTOR_Customer__c != oldAcct.i_DETECTOR_Customer__c || acct.i_DETECTOR_Units__c != oldAcct.i_DETECTOR_Units__c || 
            acct.i_REPORTER_Customer__c != oldAcct.i_REPORTER_Customer__c || acct.i_REPORTER_Units__c != oldAcct.i_REPORTER_Units__c || acct.Sales_Rep__c != oldAcct.Sales_Rep__c ||
            acct.i_OEM_Continuum__c != oldAcct.i_OEM_Continuum__c ||
            acct.i_BDR_Customer_c__c != oldAcct.i_BDR_Customer_c__c || acct.i_BDR_Exp_Date_c__c != oldAcct.i_BDR_Exp_Date_c__c || 
            acct.i_BDR_LastRun_Date_c__c != oldAcct.i_BDR_LastRun_Date_c__c || acct.i_BDR_Price_c__c != oldAcct.i_BDR_Price_c__c || 
            acct.i_BDR_Purch_Date_c__c != oldAcct.i_BDR_Purch_Date_c__c || acct.i_BDR_Report_Count_c__c != oldAcct.i_BDR_Report_Count_c__c ||
            acct.i_DetectorSDS_Customer__c != oldAcct.i_DetectorSDS_Customer__c || 
            acct.i_DetectorSDS_Exp_Date__c != oldAcct.i_DetectorSDS_Exp_Date__c || 
            acct.i_DetectorSDS_LastActivation_Date__c != oldAcct.i_DetectorSDS_LastActivation_Date__c || 
            acct.i_DetectorSDS_NumDeployed__c != oldAcct.i_DetectorSDS_NumDeployed__c || 
            acct.i_DetectorSDS_Price__c != oldAcct.i_DetectorSDS_Price__c ||
            acct.i_DetectorSDS_Purch_Date__c != oldAcct.i_DetectorSDS_Purch_Date__c ||
            acct.i_CyberHawk_ABDS_Sites__c != oldacct.i_CyberHawk_ABDS_Sites__c ||
            acct.i_CyberHawk_BDS_Sites__c != oldacct.i_CyberHawk_BDS_Sites__c ||
            acct.i_BDR_RunCount_1y__c != oldacct.i_BDR_RunCount_1y__c ||
            acct.i_BDR_RunCount_30d__c != oldacct.i_BDR_RunCount_30d__c ||
            acct.i_BDR_RunCount_90d__c != oldacct.i_BDR_RunCount_90d__c ||
            acct.i_EA_RunCount_1y__c != oldacct.i_EA_RunCount_1y__c ||
            acct.i_EA_RunCount_30d__c != oldacct.i_EA_RunCount_30d__c ||
            acct.i_EA_RunCount_90d__c != oldacct.i_EA_RunCount_90d__c ||
            acct.i_HIPAA_RunCount_1y__c != oldacct.i_HIPAA_RunCount_1y__c ||
            acct.i_HIPAA_RunCount_30d__c != oldacct.i_HIPAA_RunCount_30d__c ||
            acct.i_HIPAA_RunCount_90d__c != oldacct.i_HIPAA_RunCount_90d__c  ||
            acct.i_NA_RunCount_1y__c != oldacct.i_NA_RunCount_1y__c ||
            acct.i_NA_RunCount_30d__c != oldacct.i_NA_RunCount_30d__c ||
            acct.i_NA_RunCount_90d__c != oldacct.i_NA_RunCount_90d__c ||
            acct.i_PCI_RunCount_1y__c != oldacct.i_PCI_RunCount_1y__c ||
            acct.i_PCI_RunCount_30d__c != oldacct.i_PCI_RunCount_30d__c ||
            acct.i_PCI_RunCount_90d__c != oldacct.i_PCI_RunCount_90d__c ||
            acct.i_SA_RunCount_1y__c != oldacct.i_SA_RunCount_1y__c ||
            acct.i_SA_RunCount_30d__c != oldacct.i_SA_RunCount_30d__c ||
            acct.i_SA_RunCount_90d__c != oldacct.i_SA_RunCount_90d__c ||
            acct.i_SQL_RunCount_1y__c != oldacct.i_SQL_RunCount_1y__c ||
            acct.i_SQL_RunCount_30d__c != oldacct.i_SQL_RunCount_30d__c ||
            acct.i_SQL_RunCount_90d__c != oldacct.i_SQL_RunCount_90d__c ||
            acct.i_Reporter_Purch_Date__c != oldacct.i_Reporter_Purch_Date__c ||
            acct.i_Reporter_Exp_Date__c != oldacct.i_Reporter_Exp_Date__c ||
            acct.i_AGP_GDPR_Units__c != oldacct.i_AGP_GDPR_Units__c ||
            acct.i_AGP_Program__c  != oldacct.i_AGP_Program__c ||
            acct.i_AGP_Purch_Date__c != oldacct.i_AGP_Purch_Date__c ||
            acct.i_AG_GDPR_Exp_Date__c != oldacct.i_AG_GDPR_Exp_Date__c ||
            acct.i_AG_GDPR_Price__c != oldacct.i_AG_GDPR_Price__c ||
            acct.i_AG_HIPAA_Customer__c != oldacct.i_AG_HIPAA_Customer__c ||
            acct.i_AG_HIPAA_Exp_Date__c != oldacct.i_AG_HIPAA_Exp_Date__c ||
            acct.i_AG_HIPAA_Price__c != oldacct.i_AG_HIPAA_Price__c ||
            acct.i_AG_HIPAA_Purch_Date__c != oldacct.i_AG_HIPAA_Purch_Date__c ||
            acct.i_AG_HIPAA_Units__c != oldacct.i_AG_HIPAA_Units__c ||
            acct.ob_Cyber_Hawk__c    != oldacct.ob_Cyber_Hawk__c  ||      
            acct.ob_Cyber_Hawk_Date__c    != oldacct.ob_Cyber_Hawk_Date__c   ||      
            acct.ob_Exchange__c    != oldacct.ob_Exchange__c      ||
            acct.ob_Exchange_Date__c    != oldacct.ob_Exchange_Date__c      ||
            acct.ob_GDPR__c    != oldacct.ob_GDPR__c      ||
            acct.ob_GDPR_Date__c    != oldacct.ob_GDPR_Date__c      ||
            acct.ob_HIPAA__c    != oldacct.ob_HIPAA__c      ||
            acct.ob_HIPAA_Date__c    != oldacct.ob_HIPAA_Date__c  ||       
            acct.ob_Inspector__c    != oldacct.ob_Inspector__c    ||
            acct.ob_Inspector_Date__c    != oldacct.ob_Inspector_Date__c ||
            acct.ob_NAM_SAM__c    != oldacct.ob_NAM_SAM__c ||       
            acct.ob_NAM_SAM_Date__c    != oldacct.ob_NAM_SAM_Date__c ||      
            acct.ob_PCI__c    != oldacct.ob_PCI__c ||       
            acct.ob_PCI_Date__c    != oldacct.ob_PCI_Date__c ||      
            acct.ob_Reporter__c    != oldacct.ob_Reporter__c ||      
            acct.ob_Reporter_Date__c    != oldacct.ob_Reporter_Date__c ||    
            acct.ob_SQL__c    != oldacct.ob_SQL__c ||
            acct.ob_SQL_Date__c    != oldacct.ob_SQL_Date__c  ||
            acct.Lead_Status__c != oldacct.Lead_Status__c   ||
            acct.Lead_Status_Reason__c != oldacct.Lead_Status_Reason__c ||
            acct.Already_Being_Worked__c != oldacct.Already_Being_Worked__c ||
            acct.i_DWID_Customer__c != oldacct.i_DWID_Customer__c ||
            acct.i_DWID_Exp_Date__c != oldacct.i_DWID_Exp_Date__c ||
            acct.i_DWID_Level__c != oldacct.i_DWID_Level__c ||
            acct.i_DWID_Price__c != oldacct.i_DWID_Price__c ||
            acct.i_DWID_Purch_Date__c != oldacct.i_DWID_Purch_Date__c ||
            acct.i_AG_CI_Customer__c != oldacct.i_AG_CI_Customer__c ||
            acct.i_AG_CI_Exp_Date__c != oldacct.i_AG_CI_Exp_Date__c ||
            acct.i_AG_CI_Price__c != oldacct.i_AG_CI_Price__c ||
            acct.i_AG_CI_Purch_Date__c != oldacct.i_AG_CI_Purch_Date__c ||
            acct.i_AG_CI_Units__c != oldacct.i_AG_CI_Units__c 
            ) {

                accounts.add(acct);
        }
    }

    ProactiveWatchHelper.UpdateContactDetails(accounts);

 
Ravi Dutt SharmaRavi Dutt Sharma
Hey Ti,

A thumb rule to remeber is that long running operations should never be done synchronously. An account can have any number of contacts. For an account with few contacts, say around 50, this code may work without any issues. But for an account, where the number of contacts are high, this approach will definitely fail. The reason is, the higher the number of contacts, the higher will be the number of iterations and hence CPU time limit exception will come. 

Fortunately, you have a simple fix for this problem. You need to move the logic present in ProactiveWatchHelper.UpdateContactDetails into a batch class. Let me know if you need more help on this. Thanks.
Ti Saunders 8Ti Saunders 8
Hi Ravi,

Thanks for the insight. I'm not quite sure how to move the logic into a batch class. Here is what we have in ProactiveWatchHelper.UpdateContactDetails
public with sharing class ProactiveWatchHelper {

    public static boolean isAccountMerge = false;
    public static map<Id, Account> mapAccount = new map<Id, Account>();

    public static list<Contact> UpdateContactDetails(list<Account> accounts) {

        if (accounts == null || accounts.isEmpty()) return null;

        for (Account a : accounts) mapAccount.put(a.Id, a);

        return UpdateContactDetails(getContacts(mapAccount.keySet()));
    }

    public static String baseContactQuery = 'Select AccountId,' +
                                                    'i_Account_Type__c, i_PSA_CRM__c, i_RMM__c, i_ET_Customer__c, i_ET_Exp_Date__c, i_NA_Customer__c,' +
                                                    'i_NA_Exp_Date__c, i_NA_LastRun_Date__c, i_NA_Purch_Date__c, i_NA_Report_Count__c, i_PW_Customer__c,' +
                                                    'i_PW_Exp_Date__c, i_SA_Customer__c, i_SA_Exp_Date__c, i_SA_LastRun_Date__c, i_SA_Purch_Date__c,' +
                                                    'i_SA_Report_Count__c, i_TD_Customer__c, i_TD_Exp_Date__c,' +
                                                    'i_Type_of_IT__c, i_ET_Price__c, i_TD_Price__c, i_SA_Price__c, i_PW_Price__c, i_NA_Price__c,' +                             
                                                    'i_EA_Customer__c, i_EA_Exp_Date__c, i_EA_LastRun_Date__c, i_EA_Purch_Date__c, i_EA_Report_Count__c,' +
                                                    'i_EA_Price__c, i_ND_Module_Count__c, i_CC_Exp_Date__c, i_ET_Purch_Date__c, i_Affiliations__c,' +
                                                    'i_SQL_Customer__c, i_SQL_Exp_Date__c, i_SQL_LastRun_Date__c, i_SQL_Price__c, i_SQL_Purch_Date__c, i_SQL_Report_Count__c,' +
                                                    'i_CNCT_Customer__c, i_CNCT_Purch_Date__c, i_CNCT_Exp_Date__c, i_CNCT_Price__c,' +
                                                    'i_HIPAA_Customer__c, i_HIPAA_Exp_Date__c, i_HIPAA_Price__c, i_HIPAA_Purch_Date__c,' +
                                                    'i_INSPECTOR_Customer__c, i_INSPECTOR_Units__c, Affiliation_ASCII__c, Affiliation_CharTec__c,' +
                                                    'Affiliation_CompTIA__c, Affiliation_HTG__c, Affiliation_RR__c,' +
                                                    'Business_Type__c, i_FMIT_ID__c, i_HIPAA_Report_Count__c, i_HIPAA_LastRun_Date__c,' +
                                                    'i_NDS_HC_Customer__c, i_NDS_HC_Exp_Date__c, i_NDS_HC_Purch_Date__c, i_NDS_HC_Price__c, i_NDS_HC_Report_Count__c, i_NDS_HC_LastRun_Date__c,' +
                                                    'i_PCI_Customer__c, i_PCI_Exp_Date__c, i_PCI_LastRun_Date__c, i_PCI_Price__c, i_PCI_Purch_Date__c, i_PCI_Report_Count__c,' +
                                                    'i_MSA_Customer__c, i_MSA_Exp_Date__c, i_MSA_LastRun_Date__c, i_MSA_Price__c, i_MSA_Purch_Date__c, i_MSA_Report_Count__c,' +
                                                    'i_DETECTOR_Customer__c, i_DETECTOR_Units__c, i_REPORTER_Customer__c, i_REPORTER_Units__c, Sales_Rep__c, i_OEM_Continuum__c,' +
                                                    'i_BDR_Customer_c__c, i_BDR_Exp_Date_c__c, i_BDR_LastRun_Date_c__c, i_BDR_Price_c__c, i_BDR_Purch_Date_c__c, i_BDR_Report_Count_c__c,' +
                                                    'i_DetectorSDS_Customer__c, i_DetectorSDS_Exp_Date__c, i_DetectorSDS_LastActivation_Date__c, i_DetectorSDS_NumDeployed__c, ' +
                                                    'i_DetectorSDS_Price__c, i_DetectorSDS_Purch_Date__c, i_CyberHawk_ABDS_Sites__c, i_CyberHawk_BDS_Sites__c,' +
                                                    'i_BDR_RunCount_1y__c, i_BDR_RunCount_30d__c, i_BDR_RunCount_90d__c,' +
                                                    'i_EA_RunCount_1y__c, i_EA_RunCount_30d__c, i_EA_RunCount_90d__c,' +
                                                    'i_HIPAA_RunCount_1y__c, i_HIPAA_RunCount_30d__c, i_HIPAA_RunCount_90d__c,' +
                                                    'i_NA_RunCount_1y__c, i_NA_RunCount_30d__c, i_NA_RunCount_90d__c,' +
                                                    'i_PCI_RunCount_1y__c, i_PCI_RunCount_30d__c, i_PCI_RunCount_90d__c,' +
                                                    'i_Reporter_Purch_Date__c, i_Reporter_Exp_Date__c,' +
                                                    'i_SA_RunCount_1y__c, i_SA_RunCount_30d__c, i_SA_RunCount_90d__c,' +
                                                    'i_SQL_RunCount_1y__c, i_SQL_RunCount_30d__c, i_SQL_RunCount_90d__c,' +
                                                    'i_AGP_GDPR_Units__c, i_AGP_Program__c, i_AGP_Purch_Date__c, i_AG_GDPR_Exp_Date__c, i_AG_GDPR_Price__c,' + 
                                                    'i_AG_HIPAA_Customer__c, i_AG_HIPAA_Exp_Date__c, i_AG_HIPAA_Price__c, i_AG_HIPAA_Purch_Date__c, i_AG_HIPAA_Units__c,' +  
                                                    'Lead_Status__c, Lead_Status_Reason__c, Already_Being_Worked__c,' +
                                                    'i_DWID_Customer__c, i_DWID_Exp_Date__c, i_DWID_Level__c, i_DWID_Price__c, i_DWID_Purch_Date__c, i_AG_CI_Customer__c, i_AG_CI_Exp_Date__c, i_AG_CI_Price__c, i_AG_CI_Purch_Date__c, i_AG_CI_Units__c'+                                                
                                              ' from Contact where isDeleted = false';

    public static list<Contact> getContacts(set<Id> setIds) {
        list<Contact> contacts;
        if (setIds != null && !setIds.isEmpty()) {
            contacts = Database.query(baseContactQuery + ' and (Id in : setIds OR AccountId in : setIds)');
            system.debug(' ### contacts in getContacts ' + contacts.size() + ' => ' + contacts);
        }

        return contacts;
    }

    public static list<Contact> UpdateContactDetails(list<Contact> contacts) {

        if (contacts == null || contacts.isEmpty()) return null;

        list<Contact> contacts_To_update = new list<Contact>();

        try {

            If (mapAccount.isEmpty()) {
                for (Contact c : contacts) mapAccount.put(c.AccountId, null);

                for (Account a : [select Id,i_Account_Type__c, i_PSA_CRM__c, i_RMM__c, i_ET_Customer__c, i_ET_Exp_Date__c, i_NA_Customer__c,
                                            i_NA_Exp_Date__c, i_NA_LastRun_Date__c, i_NA_Purch_Date__c, i_NA_Report_Count__c, i_PW_Customer__c,
                                            i_PW_Exp_Date__c, i_SA_Customer__c, i_SA_Exp_Date__c, i_SA_LastRun_Date__c, i_SA_Purch_Date__c,
                                            i_SA_Report_Count__c, i_TD_Customer__c, i_TD_Exp_Date__c,
                                            i_Type_of_IT__c, i_ET_Price__c, i_TD_Price__c, i_SA_Price__c, i_PW_Price__c, i_NA_Price__c,                             
                                            i_EA_Customer__c, i_EA_Exp_Date__c, i_EA_LastRun_Date__c, i_EA_Purch_Date__c, i_EA_Report_Count__c,
                                            i_EA_Price__c, i_ND_Module_Count__c, i_CC_Exp_Date__c, i_ET_Purch_Date__c, i_Affiliations__c,
                                            i_SQL_Customer__c, i_SQL_Exp_Date__c, i_SQL_LastRun_Date__c, i_SQL_Price__c, i_SQL_Purch_Date__c, i_SQL_Report_Count__c,
                                            i_CNCT_Customer__c, i_CNCT_Purch_Date__c, i_CNCT_Exp_Date__c, i_CNCT_Price__c,
                                            i_HIPAA_Customer__c, i_HIPAA_Exp_Date__c, i_HIPAA_Price__c, i_HIPAA_Purch_Date__c,
                                            i_INSPECTOR_Customer__c, i_INSPECTOR_Units__c, Affiliation_ASCII__c, Affiliation_CharTec__c,
                                            Affiliation_CompTIA__c, Affiliation_HTG__c, Affiliation_RR__c,
                                            Business_Type__c, FMIT_ID__c, i_HIPAA_Report_Count__c, i_HIPAA_LastRun_Date__c,
                                            i_NDS_HC_Customer__c, i_NDS_HC_Exp_Date__c, i_NDS_HC_Purch_Date__c, i_NDS_HC_Price__c, i_NDS_HC_Report_Count__c, i_NDS_HC_LastRun_Date__c,
                                            i_PCI_Customer__c, i_PCI_Exp_Date__c, i_PCI_LastRun_Date__c, i_PCI_Price__c, i_PCI_Purch_Date__c, i_PCI_Report_Count__c,
                                            i_MSA_Customer__c, i_MSA_Exp_Date__c, i_MSA_LastRun_Date__c, i_MSA_Price__c, i_MSA_Purch_Date__c, i_MSA_Report_Count__c,
                                            i_DETECTOR_Customer__c, i_DETECTOR_Units__c, i_REPORTER_Customer__c, Sales_Rep__c, i_OEM_Continuum__c,
                                            i_BDR_Customer_c__c, i_BDR_Exp_Date_c__c, i_BDR_LastRun_Date_c__c, i_BDR_Price_c__c, i_BDR_Purch_Date_c__c, i_BDR_Report_Count_c__c,
                                            i_DetectorSDS_Customer__c, i_DetectorSDS_Exp_Date__c, i_DetectorSDS_LastActivation_Date__c, i_DetectorSDS_NumDeployed__c, 
                                            i_DetectorSDS_Price__c, i_DetectorSDS_Purch_Date__c, i_CyberHawk_ABDS_Sites__c, i_CyberHawk_BDS_Sites__c, i_BDR_RunCount_1y__c, i_BDR_RunCount_30d__c, i_BDR_RunCount_90d__c, 
                                            i_EA_RunCount_1y__c, i_EA_RunCount_30d__c, i_EA_RunCount_90d__c,
                                            i_HIPAA_RunCount_1y__c, i_HIPAA_RunCount_30d__c, i_HIPAA_RunCount_90d__c,
                                            i_NA_RunCount_1y__c, i_NA_RunCount_30d__c, i_NA_RunCount_90d__c,
                                            i_PCI_RunCount_1y__c, i_PCI_RunCount_30d__c, i_PCI_RunCount_90d__c,
                                            i_Reporter_Units__c, i_Reporter_Purch_Date__c, i_Reporter_Exp_Date__c,
                                            i_SA_RunCount_1y__c, i_SA_RunCount_30d__c, i_SA_RunCount_90d__c,
                                            i_SQL_RunCount_1y__c, i_SQL_RunCount_30d__c, i_SQL_RunCount_90d__c,
                                            i_AGP_GDPR_Units__c, i_AGP_Program__c, i_AGP_Purch_Date__c, i_AG_GDPR_Exp_Date__c, i_AG_GDPR_Price__c,
                                            i_AG_HIPAA_Customer__c, i_AG_HIPAA_Exp_Date__c, i_AG_HIPAA_Price__c, i_AG_HIPAA_Purch_Date__c, i_AG_HIPAA_Units__c,
                                            Lead_Status__c, Lead_Status_Reason__c, Already_Being_Worked__c, 
                                            i_DWID_Customer__c, i_DWID_Exp_Date__c, i_DWID_Level__c, i_DWID_Price__c, i_DWID_Purch_Date__c, i_AG_CI_Customer__c, i_AG_CI_Exp_Date__c, i_AG_CI_Price__c, i_AG_CI_Purch_Date__c, i_AG_CI_Units__c
                                  from   Account where Id in : mapAccount.keySet()]) {
                    mapAccount.put(a.Id, a);
                }
            }
            system.debug(' ### mapAccount ' + mapAccount.size() + ' => ' + mapAccount);
            //system.debug(' ### mapAccount.values().size ' + mapAccount.values().size() + ' => ' + mapAccount.values());

            for (Contact c : contacts) {
                Account acct = mapAccount.get(c.AccountId);

                if (acct != null) {
                    c.i_Account_Type__c = acct.i_Account_Type__c;
                    c.i_PSA_CRM__c = acct.i_PSA_CRM__c;
                    c.i_RMM__c = acct.i_RMM__c;
    
                    c.i_ET_Customer__c = acct.i_ET_Customer__c;
                    c.i_ET_Exp_Date__c = acct.i_ET_Exp_Date__c;
                    c.i_NA_Customer__c = acct.i_NA_Customer__c;
                    c.i_NA_Exp_Date__c = acct.i_NA_Exp_Date__c;
                    c.i_NA_LastRun_Date__c = acct.i_NA_LastRun_Date__c;
                    c.i_NA_Purch_Date__c = acct.i_NA_Purch_Date__c;
                    c.i_NA_Report_Count__c = acct.i_NA_Report_Count__c;
                    c.i_PW_Customer__c = acct.i_PW_Customer__c;
                    c.i_PW_Exp_Date__c = acct.i_PW_Exp_Date__c;
                    c.i_SA_Customer__c = acct.i_SA_Customer__c;
                    c.i_SA_Exp_Date__c = acct.i_SA_Exp_Date__c;
                    c.i_SA_LastRun_Date__c = acct.i_SA_LastRun_Date__c;
                    c.i_SA_Purch_Date__c = acct.i_SA_Purch_Date__c;
                    c.i_SA_Report_Count__c = acct.i_SA_Report_Count__c;
                    c.i_TD_Customer__c = acct.i_TD_Customer__c;
                    c.i_TD_Exp_Date__c = acct.i_TD_Exp_Date__c;

                    c.i_Type_of_IT__c = acct.i_Type_of_IT__c;
                    c.i_ET_Price__c = acct.i_ET_Price__c;
                    c.i_TD_Price__c = acct.i_TD_Price__c;
                    c.i_SA_Price__c = acct.i_SA_Price__c;
                    c.i_PW_Price__c = acct.i_PW_Price__c;
                    c.i_NA_Price__c = acct.i_NA_Price__c;
                    c.i_EA_Customer__c = acct.i_EA_Customer__c;
                    c.i_EA_Exp_Date__c = acct.i_EA_Exp_Date__c;
                    c.i_EA_LastRun_Date__c = acct.i_EA_LastRun_Date__c;
                    c.i_EA_Purch_Date__c = acct.i_EA_Purch_Date__c;
                    c.i_EA_Report_Count__c = acct.i_EA_Report_Count__c;
                    c.i_EA_Price__c = acct.i_EA_Price__c;
                    c.i_ND_Module_Count__c = acct.i_ND_Module_Count__c;
                    c.i_CC_Exp_Date__c = acct.i_CC_Exp_Date__c;
                    c.i_ET_Purch_Date__c = acct.i_ET_Purch_Date__c;
                    c.i_Affiliations__c = acct.i_Affiliations__c;

                    c.i_SQL_Customer__c = acct.i_SQL_Customer__c;
                    c.i_SQL_Exp_Date__c = acct.i_SQL_Exp_Date__c;
                    c.i_SQL_LastRun_Date__c = acct.i_SQL_LastRun_Date__c;
                    c.i_SQL_Price__c = acct.i_SQL_Price__c;
                    c.i_SQL_Purch_Date__c = acct.i_SQL_Purch_Date__c;
                    c.i_SQL_Report_Count__c = acct.i_SQL_Report_Count__c;

                    c.i_CNCT_Customer__c = acct.i_CNCT_Customer__c;
                    c.i_CNCT_Purch_Date__c = acct.i_CNCT_Purch_Date__c;
                    c.i_CNCT_Exp_Date__c = acct.i_CNCT_Exp_Date__c;
                    c.i_CNCT_Price__c = acct.i_CNCT_Price__c;

                    c.i_HIPAA_Customer__c = acct.i_HIPAA_Customer__c; 
                    c.i_HIPAA_Exp_Date__c = acct.i_HIPAA_Exp_Date__c;
                    c.i_HIPAA_Price__c = acct.i_HIPAA_Price__c;
                    c.i_HIPAA_Purch_Date__c = acct.i_HIPAA_Purch_Date__c;

                    c.i_INSPECTOR_Customer__c = acct.i_INSPECTOR_Customer__c;
                    c.i_INSPECTOR_Units__c = acct.i_INSPECTOR_Units__c;
                    c.Affiliation_ASCII__c = acct.Affiliation_ASCII__c;
                    c.Affiliation_CharTec__c = acct.Affiliation_CharTec__c;
                    c.Affiliation_CompTIA__c = acct.Affiliation_CompTIA__c;
                    c.Affiliation_HTG__c = acct.Affiliation_HTG__c;
                    c.Affiliation_RR__c = acct.Affiliation_RR__c;

                    c.Business_Type__c = acct.Business_Type__c; 
                    c.i_FMIT_ID__c = acct.FMIT_ID__c;
                    c.i_HIPAA_Report_Count__c = acct.i_HIPAA_Report_Count__c;
                    c.i_HIPAA_LastRun_Date__c = acct.i_HIPAA_LastRun_Date__c;
                    c.i_NDS_HC_Customer__c = acct.i_NDS_HC_Customer__c;
                    c.i_NDS_HC_Exp_Date__c = acct.i_NDS_HC_Exp_Date__c;
                    c.i_NDS_HC_Purch_Date__c = acct.i_NDS_HC_Purch_Date__c;
                    c.i_NDS_HC_Price__c = acct.i_NDS_HC_Price__c;
                    c.i_NDS_HC_Report_Count__c = acct.i_NDS_HC_Report_Count__c;
                    c.i_NDS_HC_LastRun_Date__c = acct.i_NDS_HC_LastRun_Date__c;

                    c.i_PCI_Customer__c = acct.i_PCI_Customer__c;
                    c.i_PCI_Exp_Date__c = acct.i_PCI_Exp_Date__c;
                    c.i_PCI_LastRun_Date__c = acct.i_PCI_LastRun_Date__c;
                    c.i_PCI_Price__c = acct.i_PCI_Price__c;
                    c.i_PCI_Purch_Date__c = acct.i_PCI_Purch_Date__c;
                    c.i_PCI_Report_Count__c = acct.i_PCI_Report_Count__c;
                    c.i_MSA_Customer__c = acct.i_MSA_Customer__c;
                    c.i_MSA_Exp_Date__c = acct.i_MSA_Exp_Date__c;
                    c.i_MSA_LastRun_Date__c = acct.i_MSA_LastRun_Date__c;
                    c.i_MSA_Price__c = acct.i_MSA_Price__c;
                    c.i_MSA_Purch_Date__c = acct.i_MSA_Purch_Date__c;
                    c.i_MSA_Report_Count__c = acct.i_MSA_Report_Count__c;

                    c.i_DETECTOR_Customer__c = acct.i_DETECTOR_Customer__c; 
                    c.i_DETECTOR_Units__c = acct.i_DETECTOR_Units__c;
                    c.i_REPORTER_Customer__c = acct.i_REPORTER_Customer__c; 
                    c.i_REPORTER_Units__c = acct.i_REPORTER_Units__c;
                    c.Sales_Rep__c = acct.Sales_Rep__c;
                    c.i_OEM_Continuum__c = acct.i_OEM_Continuum__c;

                    c.i_BDR_Customer_c__c = acct.i_BDR_Customer_c__c;
                    c.i_BDR_Exp_Date_c__c = acct.i_BDR_Exp_Date_c__c;
                    c.i_BDR_LastRun_Date_c__c = acct.i_BDR_LastRun_Date_c__c;
                    c.i_BDR_Price_c__c = acct.i_BDR_Price_c__c;
                    c.i_BDR_Purch_Date_c__c = acct.i_BDR_Purch_Date_c__c;
                    c.i_BDR_Report_Count_c__c = acct.i_BDR_Report_Count_c__c;

                    c.i_DetectorSDS_Customer__c = acct.i_DetectorSDS_Customer__c;
                    c.i_DetectorSDS_Exp_Date__c = acct.i_DetectorSDS_Exp_Date__c;
                    c.i_DetectorSDS_LastActivation_Date__c = acct.i_DetectorSDS_LastActivation_Date__c;
                    c.i_DetectorSDS_NumDeployed__c = acct.i_DetectorSDS_NumDeployed__c;
                    c.i_DetectorSDS_Price__c = acct.i_DetectorSDS_Price__c;
                    c.i_DetectorSDS_Purch_Date__c = acct.i_DetectorSDS_Purch_Date__c;
                    c.i_CyberHawk_ABDS_Sites__c = acct.i_CyberHawk_ABDS_Sites__c;
                    c.i_CyberHawk_BDS_Sites__c = acct.i_CyberHawk_BDS_Sites__c;
                    c.i_BDR_RunCount_1y__c = acct.i_BDR_RunCount_1y__c;
                    c.i_BDR_RunCount_30d__c = acct.i_BDR_RunCount_30d__c;
                    c.i_BDR_RunCount_90d__c = acct.i_BDR_RunCount_90d__c;
                    c.i_EA_RunCount_1y__c = acct.i_EA_RunCount_1y__c;
                    c.i_EA_RunCount_30d__c = acct.i_EA_RunCount_30d__c;
                    c.i_EA_RunCount_90d__c = acct.i_EA_RunCount_90d__c;
                    c.i_HIPAA_RunCount_1y__c = acct.i_HIPAA_RunCount_1y__c;
                    c.i_HIPAA_RunCount_30d__c = acct.i_HIPAA_RunCount_30d__c;
                    c.i_HIPAA_RunCount_90d__c = acct.i_HIPAA_RunCount_90d__c;
                    c.i_NA_RunCount_1y__c = acct.i_NA_RunCount_1y__c;
                    c.i_NA_RunCount_30d__c = acct.i_NA_RunCount_30d__c;
                    c.i_NA_RunCount_90d__c = acct.i_NA_RunCount_90d__c;
                    c.i_PCI_RunCount_1y__c = acct.i_PCI_RunCount_1y__c;
                    c.i_PCI_RunCount_30d__c = acct.i_PCI_RunCount_30d__c;
                    c.i_PCI_RunCount_90d__c = acct.i_PCI_RunCount_90d__c;
                    c.i_Reporter_Units__c = acct.i_Reporter_Units__c;
                    c.i_SA_RunCount_1y__c = acct.i_SA_RunCount_1y__c;
                    c.i_SA_RunCount_30d__c = acct.i_SA_RunCount_30d__c;
                    c.i_SA_RunCount_90d__c = acct.i_SA_RunCount_90d__c;
                    c.i_SQL_RunCount_1y__c = acct.i_SQL_RunCount_1y__c;
                    c.i_SQL_RunCount_30d__c = acct.i_SQL_RunCount_30d__c;
                    c.i_SQL_RunCount_90d__c = acct.i_SQL_RunCount_90d__c;
                    c.i_Reporter_Purch_Date__c = acct.i_Reporter_Purch_Date__c;
                    c.i_Reporter_Exp_Date__c = acct.i_Reporter_Exp_Date__c;
                    c.i_AGP_GDPR_Units__c = acct.i_AGP_GDPR_Units__c;
                    c.i_AGP_Program__c  = acct.i_AGP_Program__c;
                    c.i_AGP_Purch_Date__c = acct.i_AGP_Purch_Date__c;
                    c.i_AG_GDPR_Exp_Date__c = acct.i_AG_GDPR_Exp_Date__c;
                    c.i_AG_GDPR_Price__c = acct.i_AG_GDPR_Price__c;
                    c.i_AG_HIPAA_Customer__c = acct.i_AG_HIPAA_Customer__c;
                    c.i_AG_HIPAA_Exp_Date__c = acct.i_AG_HIPAA_Exp_Date__c;
                    c.i_AG_HIPAA_Price__c = acct.i_AG_HIPAA_Price__c;
                    c.i_AG_HIPAA_Purch_Date__c = acct.i_AG_HIPAA_Purch_Date__c;
                    c.i_AG_HIPAA_Units__c = acct.i_AG_HIPAA_Units__c;                    
                    c.Lead_Status__c = acct.Lead_Status__c; 
                    c.Lead_Status_Reason__c = acct.Lead_Status_Reason__c;        
                    c.Already_Being_Worked__c = acct.Already_Being_Worked__c;
                    c.i_DWID_Customer__c = acct.i_DWID_Customer__c;
                    c.i_DWID_Exp_Date__c = acct.i_DWID_Exp_Date__c;
                    c.i_DWID_Level__c = acct.i_DWID_Level__c;
                    c.i_DWID_Price__c = acct.i_DWID_Price__c;
                    c.i_DWID_Purch_Date__c = acct.i_DWID_Purch_Date__c;
                    c.i_AG_CI_Customer__c = acct.i_AG_CI_Customer__c;
                    c.i_AG_CI_Exp_Date__c = acct.i_AG_CI_Exp_Date__c;
                    c.i_AG_CI_Price__c = acct.i_AG_CI_Price__c;
                    c.i_AG_CI_Purch_Date__c = acct.i_AG_CI_Purch_Date__c;
                    c.i_AG_CI_Units__c = acct.i_AG_CI_Units__c;

                    If (!trigger.isExecuting || isAccountMerge || (trigger.isAfter && trigger.isUpdate)) {
                        contacts_To_Update.add(c);
                    }
                }
            }

            system.debug(' ### contacts_To_Update ' + contacts_To_Update.size() + ' => ' + contacts_To_Update);
            If (!contacts_To_Update.isEmpty()) {
                update contacts_To_Update;
                return contacts_To_Update;
            }

        } catch (Exception ex) {
            //system.debug(' ### exception in UpdateContactDetails ' + ex.getMessage());
            throw ex;
        }
        return contacts;
    }   
}

 
Ravi Dutt SharmaRavi Dutt Sharma
Just move your query inside the start method of batch class and rest of the logic inside the execute method. You can find more information about batch class here (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm)