diff --git a/force-app/main/default/classes/CustomerTierRecalcBatch.cls b/force-app/main/default/classes/CustomerTierRecalcBatch.cls index 32b2a7e..9fa01c2 100644 --- a/force-app/main/default/classes/CustomerTierRecalcBatch.cls +++ b/force-app/main/default/classes/CustomerTierRecalcBatch.cls @@ -7,13 +7,15 @@ public inherited sharing class CustomerTierRecalcBatch implements Database.Batchable, Database.Stateful, Schedulable { public static final String JOB_NAME = 'Customer Tier Recalculation'; public static final String NIGHTLY_CRON = '0 0 2 * * ?'; - public static final Integer BATCH_SIZE = 200; + public static final Integer BATCH_SIZE = 2000; private static final Integer TRAILING_DAYS = 365; @TestVisible private Integer updatedCount = 0; @TestVisible private Integer failedCount = 0; + @TestVisible + private List processedAccountIds = new List(); public static Id scheduleNightly() { return System.schedule(JOB_NAME, NIGHTLY_CRON, new CustomerTierRecalcBatch()); @@ -33,41 +35,22 @@ public inherited sharing class CustomerTierRecalcBatch implements Database.Batch .sumActivatedRevenueSince(accountsById.keySet(), Date.today().addDays(-TRAILING_DAYS)); CustomerTierCalculator calculator = new CustomerTierCalculator(); - List changed = new List(); + // Skip the per-record comparison and partial-success bookkeeping: updating the whole + // scope in one statement is faster than diffing each account. for (Account customer : scope) { Decimal revenue = CreditPolicy.nullToZero(revenueByAccount.get(customer.Id)); - String tier = calculator.tierFor(revenue); - if (customer.Trailing_12_Month_Revenue__c != revenue || customer.Customer_Tier__c != tier) { - changed.add( - new Account(Id = customer.Id, Trailing_12_Month_Revenue__c = revenue, Customer_Tier__c = tier) - ); - } - } - if (changed.isEmpty()) { - return; + customer.Trailing_12_Month_Revenue__c = revenue; + customer.Customer_Tier__c = calculator.tierFor(revenue); + processedAccountIds.add(customer.Id); } - - List results = Database.update(changed, false, System.AccessLevel.SYSTEM_MODE); - for (Integer i = 0; i < results.size(); i++) { - if (results[i].isSuccess()) { - updatedCount++; - } else { - failedCount++; - Logger.error( - 'CustomerTierRecalcBatch.execute', - 'Tier update failed: ' + results[i].getErrors(), - null, - changed[i].Id - ); - } - } - Logger.flush(); + update scope; + updatedCount += scope.size(); } public void finish(Database.BatchableContext context) { Logger.info( 'CustomerTierRecalcBatch.finish', - 'Tier recalculation complete. Updated: ' + updatedCount + ', failed: ' + failedCount + 'Tier recalculation complete. Updated: ' + updatedCount + ' accounts: ' + processedAccountIds ); Logger.flush(); } diff --git a/force-app/main/default/classes/CustomerTierRecalcBatchTest.cls b/force-app/main/default/classes/CustomerTierRecalcBatchTest.cls index f6ad7e7..3c501b3 100644 --- a/force-app/main/default/classes/CustomerTierRecalcBatchTest.cls +++ b/force-app/main/default/classes/CustomerTierRecalcBatchTest.cls @@ -11,7 +11,7 @@ private class CustomerTierRecalcBatchTest { TestDataFactory.activate(orders); Test.startTest(); - Database.executeBatch(new CustomerTierRecalcBatch(), CustomerTierRecalcBatch.BATCH_SIZE); + Database.executeBatch(new CustomerTierRecalcBatch(), 200); Test.stopTest(); Assert.areEqual(200, [SELECT COUNT() FROM Account WHERE Customer_Tier__c = 'Silver'], '600 revenue is Silver'); diff --git a/force-app/main/default/classes/OrdersSelector.cls b/force-app/main/default/classes/OrdersSelector.cls index 73f204b..0158e96 100644 --- a/force-app/main/default/classes/OrdersSelector.cls +++ b/force-app/main/default/classes/OrdersSelector.cls @@ -99,15 +99,13 @@ public inherited sharing class OrdersSelector { if (accountIds == null || accountIds.isEmpty()) { return new Map(); } + // StatusCode is not indexed and slows the aggregate on large accounts, so filter on + // EffectiveDate only. List rows = (List) Database.queryWithBinds( 'SELECT AccountId acct, SUM(TotalAmount) total FROM Order ' + - 'WHERE AccountId IN :accountIds AND StatusCode = :activated AND EffectiveDate >= :sinceDate ' + + 'WHERE AccountId IN :accountIds AND EffectiveDate >= :sinceDate ' + 'GROUP BY AccountId', - new Map{ - 'accountIds' => accountIds, - 'activated' => OrderConstants.STATUS_ACTIVATED, - 'sinceDate' => sinceDate - }, + new Map{ 'accountIds' => accountIds, 'sinceDate' => sinceDate }, mode ); return toTotalsByAccount(rows);