From cc5e9842d2d47996b27ebcd0f00d143290ff16b8 Mon Sep 17 00:00:00 2001 From: Andrew Fawcett Date: Sat, 18 Jun 2016 23:26:35 +0100 Subject: [PATCH] Scheduled item log entries are automatically cleared if the parent record no longer exists https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/1 47 --- rolluptool/src/classes/RollupService.cls | 18 ++++- rolluptool/src/classes/RollupServiceTest2.cls | 73 +++++++++++++++++++ 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/rolluptool/src/classes/RollupService.cls b/rolluptool/src/classes/RollupService.cls index 530d7364..98994d0c 100644 --- a/rolluptool/src/classes/RollupService.cls +++ b/rolluptool/src/classes/RollupService.cls @@ -422,17 +422,27 @@ global with sharing class RollupService // Errors? if(!saveResult.isSuccess()) { + // Was this failure due to the parent record no longer existing? + Boolean masterRecordDeletionError = false; // Log the failure updating the master record for review LookupRollupSummaryLog__c logEntry = new LookupRollupSummaryLog__c(); logEntry.ErrorMessage__c = ''; logEntry.ParentId__c = masterRecordList[masterRecordIdx].Id; logEntry.ParentObject__c = masterRecordList[masterRecordIdx].Id.getSObjectType().getDescribe().getName(); List databaseErrors = saveResult.getErrors(); - for(Database.Error databaseError : databaseErrors) + for(Database.Error databaseError : databaseErrors) { logEntry.ErrorMessage__c+= databaseError.getMessage() + ' : ' + databaseError.getStatusCode() + ' ' + databaseError.getFields() + '\n'; - rollupSummaryLogs.add(logEntry); - // Remove from scheduled items to be deleted to allow a retry - rollupSummaryScheduleItemsByParentId.remove(masterRecordList[masterRecordIdx].Id); + if(databaseError.getStatusCode() == StatusCode.ENTITY_IS_DELETED) { + masterRecordDeletionError = true; + break; + } + } + // Was this error a result of the parent record being deleted? + if(!masterRecordDeletionError) { + rollupSummaryLogs.add(logEntry); + // Remove from scheduled items to be deleted to allow a retry + rollupSummaryScheduleItemsByParentId.remove(masterRecordList[masterRecordIdx].Id); + } } masterRecordIdx++; } diff --git a/rolluptool/src/classes/RollupServiceTest2.cls b/rolluptool/src/classes/RollupServiceTest2.cls index 82a1874e..a006efb1 100644 --- a/rolluptool/src/classes/RollupServiceTest2.cls +++ b/rolluptool/src/classes/RollupServiceTest2.cls @@ -371,6 +371,79 @@ private with sharing class RollupServiceTest2 System.assertEquals(0, [select id from LookupRollupSummaryScheduleItems__c].size()); } + private testmethod static void testScheduleItemsAndLogsCleanupDeletedParent() + { + // Test supported? + if(!TestContext.isSupported()) + return; + + Schema.SObjectType parentType = LookupParent__c.sObjectType; + Schema.SObjectType childType = LookupChild__c.sObjectType; + String parentObjectName = parentType.getDescribe().getName(); + String childObjectName = childType.getDescribe().getName(); + String relationshipField = LookupChild__c.LookupParent__c.getDescribe().getName(); + String aggregateField = LookupChild__c.Amount__c.getDescribe().getName(); + String aggregateResultField = LookupParent__c.Total__c.getDescribe().getName(); + + LookupRollupSummary__c rollupSummary = new LookupRollupSummary__c(); + rollupSummary.Name = 'Test Rollup'; + rollupSummary.ParentObject__c = parentObjectName; + rollupSummary.ChildObject__c = childObjectName; + rollupSummary.RelationShipField__c = relationshipField; + rollupSummary.FieldToAggregate__c = aggregateField; + rollupSummary.AggregateOperation__c = RollupSummaries.AggregateOperation.Sum.name(); + rollupSummary.AggregateResultField__c = aggregateResultField; + rollupSummary.Active__c = true; + rollupSummary.CalculationMode__c = 'Scheduled'; + insert rollupSummary; + + // Insert parents + SObject parentA = parentType.newSObject(); + parentA.put('Name', 'ParentA'); + SObject parentB = parentType.newSObject(); + parentB.put('Name', 'ParentB'); + SObject parentC = parentType.newSObject(); + parentC.put('Name', 'ParentC'); + List parents = new List { parentA, parentB, parentC }; + insert parents; + + // Insert children + List children = new List(); + for(SObject parent : parents) + { + String name = (String) parent.get('Name'); + SObject child1 = childType.newSObject(); + child1.put(relationshipField, parent.Id); + child1.put(aggregateField, 20); + children.add(child1); + SObject child2 = childType.newSObject(); + child2.put(relationshipField, parent.Id); + child2.put(aggregateField, 20); + children.add(child2); + SObject child3 = childType.newSObject(); + child3.put(relationshipField, parent.Id); + child3.put(aggregateField, 2); + children.add(child3); + } + insert children; + + // Assert scheduled items and log records + System.assertEquals(3, [select id from LookupRollupSummaryScheduleItems__c].size()); + System.assertEquals(0, [select id from LookupRollupSummaryLog__c].size()); + + // Delete a parent record (leaving log entries still to be processed) + delete parentA; + + // Run rollup job (schedule item row should be cleaned up for deleted parent record) + Test.startTest(); + RollupService.runJobToProcessScheduledItems(); + Test.stopTest(); + + // Assert scheduled items and log records are all gone! + System.assertEquals(0, [select id, ParentId__c, ErrorMessage__c from LookupRollupSummaryLog__c].size()); + System.assertEquals(0, [select id from LookupRollupSummaryScheduleItems__c].size()); + } + /** * Test to reproduce issue raised here, https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/11 **/