From 3c2f9ae48ffb498b5fc27b8a5965f99a3de07430 Mon Sep 17 00:00:00 2001 From: Andrew Fawcett Date: Sun, 1 Sep 2013 22:01:38 +0100 Subject: [PATCH] Fix for issue 4 https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/4 --- rolluptool/src/classes/LREngine.cls | 9 +- rolluptool/src/classes/RollupServiceTest.cls | 95 ++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/rolluptool/src/classes/LREngine.cls b/rolluptool/src/classes/LREngine.cls index 808af29..f213e69 100644 --- a/rolluptool/src/classes/LREngine.cls +++ b/rolluptool/src/classes/LREngine.cls @@ -118,9 +118,16 @@ public class LREngine { for (String detailFld : detail2MasterFldMap.keySet()) { Object aggregatedDetailVal = res.get(detail2AliasMap.get(detailFld)); masterObj.put(detail2MasterFldMap.get(detailFld), aggregatedDetailVal); - } + } + // Remove master Id record as its been processed + masterIds.remove(masterRecId); } + // Zero rollups for unprocessed master records (those with no longer any child relationships) + for(Id masterRecId : masterIds) + for (String detailFld : detail2MasterFldMap.keySet()) + masterRecordsMap.get(masterRecId).put(detail2MasterFldMap.get(detailFld), 0); + return masterRecordsMap.values(); } diff --git a/rolluptool/src/classes/RollupServiceTest.cls b/rolluptool/src/classes/RollupServiceTest.cls index 0aa4ee2..84312f5 100644 --- a/rolluptool/src/classes/RollupServiceTest.cls +++ b/rolluptool/src/classes/RollupServiceTest.cls @@ -278,6 +278,101 @@ private with sharing class RollupServiceTest // Assert rollup System.assertEquals(0, [select AnnualRevenue from Account where Id = :account.Id].AnnualRevenue); } + + private testmethod static void testSingleRollupWithInsertThenDelete() + { + // Test supported? + if(!TestContext.isSupported()) + return; + + // Configure rollup + LookupRollupSummary__c rollupSummary = new LookupRollupSummary__c(); + rollupSummary.Name = 'Total Opportunities greater than 200 into Annual Revenue on Account'; + rollupSummary.ParentObject__c = 'Account'; + rollupSummary.ChildObject__c = 'Opportunity'; + rollupSummary.RelationShipField__c = 'AccountId'; + rollupSummary.RelationShipCriteria__c = null; + rollupSummary.FieldToAggregate__c = 'Amount'; + rollupSummary.AggregateOperation__c = 'Sum'; + rollupSummary.AggregateResultField__c = 'AnnualRevenue'; + rollupSummary.Active__c = true; + rollupSummary.CalculationMode__c = 'Realtime'; + insert new List { rollupSummary }; + + // Test data + Account account = new Account(); + account.Name = 'Test Account'; + account.AnnualRevenue = 0; + insert account; + Opportunity opp = new Opportunity(); + opp.Name = 'Test Opportunity'; + opp.StageName = 'Open'; + opp.CloseDate = System.today(); + opp.AccountId = account.Id; + opp.Amount = 100; + insert opp; + + // Assert rollup + System.assertEquals(100, [select AnnualRevenue from Account where Id = :account.Id].AnnualRevenue); + + // Delete Opportunity + delete opp; + + // Assert rollup + System.assertEquals(0, [select AnnualRevenue from Account where Id = :account.Id].AnnualRevenue); + } + + private testmethod static void testSingleRollupWithInsertsThenDelete() + { + // Test supported? + if(!TestContext.isSupported()) + return; + + // Configure rollup + LookupRollupSummary__c rollupSummary = new LookupRollupSummary__c(); + rollupSummary.Name = 'Total Opportunities greater than 200 into Annual Revenue on Account'; + rollupSummary.ParentObject__c = 'Account'; + rollupSummary.ChildObject__c = 'Opportunity'; + rollupSummary.RelationShipField__c = 'AccountId'; + rollupSummary.RelationShipCriteria__c = null; + rollupSummary.FieldToAggregate__c = 'Amount'; + rollupSummary.AggregateOperation__c = 'Sum'; + rollupSummary.AggregateResultField__c = 'AnnualRevenue'; + rollupSummary.Active__c = true; + rollupSummary.CalculationMode__c = 'Realtime'; + insert new List { rollupSummary }; + + // Test data + Account account = new Account(); + account.Name = 'Test Account'; + account.AnnualRevenue = 0; + insert account; + { + Opportunity opp = new Opportunity(); + opp.Name = 'Test Opportunity'; + opp.StageName = 'Open'; + opp.CloseDate = System.today(); + opp.AccountId = account.Id; + opp.Amount = 100; + insert opp; + } + Opportunity opp = new Opportunity(); + opp.Name = 'Test Opportunity'; + opp.StageName = 'Open'; + opp.CloseDate = System.today(); + opp.AccountId = account.Id; + opp.Amount = 100; + insert opp; + + // Assert rollup + System.assertEquals(200, [select AnnualRevenue from Account where Id = :account.Id].AnnualRevenue); + + // Delete Opportunity + delete opp; + + // Assert rollup + System.assertEquals(100, [select AnnualRevenue from Account where Id = :account.Id].AnnualRevenue); + } private testmethod static void testRollupWithoutChanges() {