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
sfdc dev 2264sfdc dev 2264 

Trigger CPU usage limition help needed

Hi,

I need help on the following issue 

I am having a trigger on account object 

When, we trying to upload bulk records  via dataloader or ETL , system is facing 'CPU usage limition'' issue.
For one Account record creation, the trigger is running four times instead of one time.

How to avoid the issue, Kindly help me pls
MY TRIGGER :

trigger AccountTrigger on Account (after insert, before update, after update, after delete) {  
    
    /* Assign & create Account team members based on user department*/
    if(Trigger_Status__c.getValues('AccountTeam').Active__c && Trigger.isUpdate  && Trigger.isBefore ){
        /* Before insert process */
        AccountTriggerHelper.updateAccountTeam(Trigger.newMap, Trigger.oldMap);
    } 
    
    /* Trigger to invoke the AccountIneligibilty Logs Tracking*/
    if(Trigger_Status__c.getValues('AccountEligibilityLogTrigger').Active__c && Trigger.isAfter && Trigger.isUpdate){
        AccountTriggerHelper.createAccountEligiblityLogs(Trigger.newMap, Trigger.oldMap);
    }
    
    /* Trigger to invoke the DiscountCode Calculations on Products*/
    if(Trigger_Status__c.getValues('AccountAirlineLeveltrigger').Active__c && Trigger.isAfter && Trigger.isUpdate) {
        AccountTriggerHelper.calculateDiscountCode(Trigger.newMap,Trigger.oldMap);
    }
    
    /* Trigger to invoke the DeleteAccount Tracking of Records*/
    if(Trigger_Status__c.getValues('AccountDeleteTrigger').Active__c && Trigger.isAfter && Trigger.isDelete) {
        AccountTriggerHelper.createDelAccountLogTracker(Trigger.oldMap); 
    }        
}
MY CLASS:

public class AccountTriggerHelper {
    
    /*--------------------------------------------------------------------------------------      
  Method Name:        updateAccountTeam
  Description:        Method to Assign & create Account team members based on user department
  Parameter:          Account New Map & Old Map 
  --------------------------------------------------------------------------------------*/    
    public static void updateAccountTeam(Map<Id,Account> newMapAccount, Map<Id,Account> oldMapAccount){
      try{
        AccountTeamHandler accHandler = new AccountTeamHandler();
        List<Account> lstNewAccount = newMapAccount.values();
            accHandler.setRevenueManualUpdate(lstNewAccount, oldMapAccount);
            accHandler.createAccountTeamMembers(lstNewAccount, oldMapAccount);
      }
      catch(Exception ex){
        String recType = CustomSettingsUtilities.getConfigDataMap('Log Exception Logs Rec Type');    
            CreateLogs.LogWrapper logWrap = new CreateLogs.LogWrapper('Account Team - Account Trigger', 'AccountTriggerHelper', 
                                                                      'updateAccountTeam', UserInfo.getUserName(), '', 
                                                                      '', false, recType);
            
            Log__c objLogs = CreateLogs.createLogRec(logWrap, '');
            CreateLogs.createApplicationLog(objLogs, ex);
      }
    }

    /*--------------------------------------------------------------------------------------      
  Method Name:        createAccountEligiblityLogs
  Description:        Method to Create AccountIneligibilty Logs and Update
  Parameter:          Account New Map & Old Map 
  --------------------------------------------------------------------------------------*/    
    public static void createAccountEligiblityLogs(Map<Id,Account> newMapAccount, Map<Id,Account> oldMapAccount){
      try{
        AccountEligibilityLogHandler.createAccountEligiblityLogs(newMapAccount, oldMapAccount);
      }
      catch(Exception ex){
        String recType = CustomSettingsUtilities.getConfigDataMap('Log Exception Logs Rec Type');    
            CreateLogs.LogWrapper logWrap = new CreateLogs.LogWrapper('SME - Account Trigger', 'AccountTriggerHelper', 
                                                                      'createAccountEligiblityLogs', UserInfo.getUserName(), '', 
                                                                      '', false, recType);
            
            Log__c objLogs = CreateLogs.createLogRec(logWrap, '');
            CreateLogs.createApplicationLog(objLogs, ex);
      }
    }

    /*--------------------------------------------------------------------------------------      
  Method Name:        calculateDiscountCode
  Description:        Method to DiscountCode Calculations on Products
  Parameter:          Account New Map & Old Map 
  --------------------------------------------------------------------------------------*/    
    public static void calculateDiscountCode(Map<Id,Account> newMapAccount, Map<Id,Account> oldMapAccount){
    try{
            AccountAirlineLevelHandler.updateDiscountCodeOnProducts(newMapAccount, oldMapAccount);
      }
      catch(Exception ex){
        String recType = CustomSettingsUtilities.getConfigDataMap('Log Exception Logs Rec Type');    
            CreateLogs.LogWrapper logWrap = new CreateLogs.LogWrapper('SME - Account Trigger', 'AccountTriggerHelper', 
                                                                      'calculateDiscountCode', UserInfo.getUserName(), '', 
                                                                      '', false, recType);
            
            Log__c objLogs = CreateLogs.createLogRec(logWrap, '');
            CreateLogs.createApplicationLog(objLogs, ex);
      }
    }

    /*--------------------------------------------------------------------------------------      
  Method Name:        createDelAccountLogTracker
  Description:        Method to Create Delete Tracker Logs records when any Account is Deleted
  Parameter:          Account Old Map 
  --------------------------------------------------------------------------------------*/    
    public static void createDelAccountLogTracker(Map<Id,Account> oldMapAccount){
        try{
            String recTypeName = CustomSettingsUtilities.getConfigDataMap('Del Log SME Account Rec Type');
            List<CreateLogs.DeleteLogWrapper> lstDelLogWrapper = new List<CreateLogs.DeleteLogWrapper>();
            for(Account acc: oldMapAccount.values()){
                String directvalue = CustomSettingsUtilities.getConfigDataMap('AccListner Type Direct Stream');
                if(acc.Listner_Type__c == directvalue){
                    CreateLogs.DeleteLogWrapper delLogWrapper = new CreateLogs.DeleteLogWrapper();
                    delLogWrapper.recType = recTypeName;
                    delLogWrapper.recName =  acc.Name;
                    delLogWrapper.abnNumber = acc.ABN_Tax_Reference__c;
                    delLogWrapper.sobjectId = acc.Id;
                    delLogWrapper.sobjectName = 'Account';
                    lstDelLogWrapper.add(delLogWrapper);
                }
            }
            List<Delete_Tracker_Log__c> lstDelTrackerLog = CreateLogs.createDeleteLog(lstDelLogWrapper);
            insert lstDelTrackerLog;
        }catch(Exception ex){
            String recType = CustomSettingsUtilities.getConfigDataMap('Log Exception Logs Rec Type');    
            CreateLogs.LogWrapper logWrap = new CreateLogs.LogWrapper('Direct Stream - Account Trigger', 'AccountTriggerHelper', 
                                                                      'createDelAccountLogTracker', UserInfo.getUserName(), '', 
                                                                      '', false, recType);
            
            Log__c objLogs = CreateLogs.createLogRec(logWrap, '');
            CreateLogs.createApplicationLog(objLogs, ex);
        }
    }

}
Kindly help me pls

Thanks in Advance

 
Best Answer chosen by sfdc dev 2264
LBKLBK
I don't see a lot of scope for improvement in the above code.

Except that the line 78 can be moved outside the FOR loop so that it doesn't have to be called multiple times.
String directvalue = CustomSettingsUtilities.getConfigDataMap('AccListner Type Direct Stream')
This line can be added before line 77.

Unless there are other triggers on Account object that needs to be improved, you have to live with a smaller batch size.

Please mark this question as solved, if this addresses your issue.

 

All Answers

LBKLBK
What is the volume of the data you are processing?

Also, have you tried smaller batch size (say something in 2 digits)?

 
sfdc dev 2264sfdc dev 2264
for 200 its failing

Also, have you tried smaller batch size (say something in 2 digits) -> its successful

thanks
LBKLBK
I don't see a lot of scope for improvement in the above code.

Except that the line 78 can be moved outside the FOR loop so that it doesn't have to be called multiple times.
String directvalue = CustomSettingsUtilities.getConfigDataMap('AccListner Type Direct Stream')
This line can be added before line 77.

Unless there are other triggers on Account object that needs to be improved, you have to live with a smaller batch size.

Please mark this question as solved, if this addresses your issue.

 
This was selected as the best answer
sfdc dev 2264sfdc dev 2264
Thanks bro , just a small clarification , will using a static boolean variable in class help in this case


public static boolean firstRun = true;

and setting it as false in trigger after every if condition

 
Saravana Bharathi 1Saravana Bharathi 1
Is there any dml operation is happening on account object, by inserting Delete_Tracker_Log__c( Like insert new account etc..)
through process builder or workflow or trigger or by any other mean.

Check how 4 times trigger is running.

Since, its on delete operation, you are inserting Delete_Tracker_Log__c. It shouldnt fire this trigger again for delete operation.

I have faced one issue on delete operation taking too long time.
In our code, we are trying to delete a record in apex, that is taking too long time( 20 second to delete), which we got in debug log, by tracking dml start and dml end in time, its in webservice call, so request timeout error we got. We raised a case with salesforce,

So, it could be similar issue.

Try to figure is it salesforce is taking too long time to delete a record, or through process builder or any other trigger is doing recursive call.
Thanks