1

We have a custom trigger, CommissionUpdate, that causes an Apex CPU time limit error whenever we try to mass update records on the Account. I'm not a developer, and so I have no idea why this trigger is causing the problem. I've included the trigger causing the issue. Any help would be appreciated! Thanks

trigger CommissionUpdate on Account (after update) {
List<Account> commissionCheckList = new List<Account>();
for (Account a: Trigger.new){
    if (a.Sales_Rep_Commission__c != NULL){
        Boolean oldAcctPayable = Trigger.oldMap.get(a.Id).Account_Payable__c;
        Boolean newAcctPayable = a.Account_Payable__c;
        Decimal oldAcctCommission = Trigger.oldMap.get(a.Id).Commission_Value__c;
        Decimal newAcctCommission = a.Commission_Value__c;

        if ((newAcctPayable && !oldAcctPayable) || (newAcctPayable && oldAcctCommission != newAcctCommission)){
            commissionCheckList.add(a);
        }
    }
}

List<Commission__c> commsToAdd = new List<Commission__c>();
if (commissionCheckList.size() > 0){

    AggregateResult[] groupedResults = [SELECT For_Account__c,Sum(Amount__c)amt FROM Commission__c WHERE For_Account__c IN: commissionCheckList GROUP BY For_Account__c];
    Map<Id,Double> resultsMap = new Map<Id,Double>();
    for (AggregateResult ar: groupedResults){
        resultsMap.put((Id)ar.get('For_Account__c'),(Double)ar.get('amt'));
    }
    for (Account a: commissionCheckList){
        List<String> reasonForChange = new List<String>();
        Account oldA = Trigger.oldMap.get(a.Id);
        if (oldA.Billing_Type__c != a.Billing_Type__c && a.Billing_Type__c == 'Credit Card') reasonForChange.add('Billing Type Changed to Credit Card:  -1 Point');
        if (oldA.Billing_Type__c != a.Billing_Type__c && a.Billing_Type__c == 'Manual Billing') reasonForChange.add('Billing Type Changed to Manual Billing:  -3 Points');
        if (oldA.Billing_Type__c != a.Billing_Type__c && a.Billing_Type__c == 'ACH') reasonForChange.add('Billing Type Changed to ACH:  +1 Point');
        if (oldA.Points_Used__c > a.Points_Used__c) reasonForChange.add('Points Used Decreased from ' + oldA.Points_Used__c + ' to ' + a.Points_Used__c + ' Account Changes');
        if (oldA.Points_Used__c < a.Points_Used__c) reasonForChange.add('Points Used Increased from ' + oldA.Points_Used__c + ' to ' + a.Points_Used__c + ' Account Changes');
        if (oldA.Credit_Score__c != a.Credit_Score__c) reasonForChange.add('Credit Score Changed');
        if (oldA.Monitoring_Comm_Impact__c != a.Monitoring_Comm_Impact__c) reasonForChange.add('MMR Changed--Monitoring Comm Impact is now: $'+ a.Monitoring_Comm_Impact__c + ' Account Changes' );
        String reasonString = String.join(reasonForChange,'+');

        if(!resultsMap.containsKey(a.Id)){
            Commission__c c = new Commission__c(
                For_Account__c = a.Id,
                OwnerId = a.OwnerId,
                For_Sales_Rep__c = a.Sales_Rep_Commission__c,
                Amount__c = a.Commission_Value__c,
                Reason_for_Change__c = 'Original Commission'

         );
            commsToAdd.add(c);
        } else{
            Double previousCommission = resultsMap.get(a.Id);
            if (a.Commission_Value__c != NULL && (previousCommission == NULL || previousCommission != a.Commission_Value__c)){
                Commission__c c = new Commission__c();
                c.For_Account__c = a.Id;
                c.OwnerId = a.OwnerId;
                c.For_Sales_Rep__c = a.Sales_Rep_Commission__c;
                c.Amount__c = a.Commission_Value__c - (previousCommission != NULL ? previousCommission : 0);
                c.Reason_for_Change__c = (reasonString != '' ? reasonString : 'Account Changes');
                commsToAdd.add(c);
            }
        }
    }
}
insert commsToAdd;

}

  • 1
    why is the person who developed the trigger not fixing/checking it? – glls Feb 12 '18 at 17:20
  • They're a fairly big Salesforce partner, but we had a falling out because they're terrible (caused more problems than just this one), and they want a ton of money to diagnose and/or work with us. We've had a lot of discussions with Salesforce about this partner. Basically, we don't even want to talk with them any more, and they made this years ago. – Eddie Prignano Feb 12 '18 at 17:33
4

You will need to have a developer take a look at your overall system. Apex CPU governor limits apply to all logic in a transaction, not on a per-unit basis. As such, the CPU limit can occur at any point past 10,000 milliseconds of CPU usage. This does not mean that this particular trigger was at fault, only that your executed ceased here because the governor limit was exceeded, and the system decided not to expend additional resources on the transaction. For example, if this trigger only takes 1,000 ms, but another trigger takes 9,500 ms (but runs first), then this trigger could be "blamed" for the problem, even though the other trigger was actually the problem.

If you want to try to debug this yourself, open the Developer Console, go to Debug > Change Log Levels > Add/Change the Debug Level, and create a new debug level that has Profiling set to FINEST, and the other levels set to NONE. Run your transaction, and you'll see a new log in the Logs tab of the Developer Console. You can read the final lines in the log to see which methods or triggers are causing the problem. From there, you can then ask an informed question that would allow us to further assist you.

Please note that I am exceptionally convinced that this trigger is not at fault, because even with 200 rows of data, it should execute in less than one second. Even if you have Workflow Field updates on your Account object, it's not likely that you'd even get a CPU limit warning, much less an actual error.

  • Thanks... So I did that and this is what I'm seeing: CODE_UNIT_STARTED dlrs_AccountTrigger on Account trigger event AfterUpdate... A few lines down from that it says LIMIT_USAGE_FOR_NS and Maximum CPU time: 10569 out of 10000. Does this mean that it's probably the dlrs_AccountTrigger on Account causing the issue? – Eddie Prignano Feb 12 '18 at 18:39
  • That trigger, dlrs_AccountTrigger is: /** * Auto Generated and Deployed by the Declarative Lookup Rollup Summaries Tool package (dlrs) **/ trigger dlrs_AccountTrigger on Account (before delete, before insert, before update, after delete, after insert, after undelete, after update) { dlrs.RollupService.triggerHandler(); } – Eddie Prignano Feb 12 '18 at 18:39
  • Actually that wasn't the final one or the right debug log so ignore all of that – Eddie Prignano Feb 12 '18 at 18:50
  • 1
    I figured it out... The CommissionUpdate trigger was causing an issue when the Account Owner wasn't the same as the Commission Owner. I just added a bit of code to the trigger and it resolved the issue. – Eddie Prignano Feb 12 '18 at 20:39
  • 1
    @EddiePrignano Congrats. Usually most governor limit issues are pretty easy to fix; it's typically just one thing. – sfdcfox Feb 12 '18 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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