Skip to content

Commit

Permalink
Fix for two scheduled rollups with criteria not working
Browse files Browse the repository at this point in the history
See issue
#1
00
  • Loading branch information
afawcettffdc committed Dec 15, 2014
1 parent 2e19d11 commit 43a8b39
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 30 deletions.
71 changes: 41 additions & 30 deletions rolluptool/src/classes/RollupService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,18 @@ global with sharing class RollupService
}

// Map rollup summary schedule items by parent id, in order to remove only those whos parent/master record actually gets updated below
Map<Id, LookupRollupSummaryScheduleItems__c> rollupSummaryScheduleItemsByParentId =
new Map<Id, LookupRollupSummaryScheduleItems__c>();
Map<Id, List<LookupRollupSummaryScheduleItems__c>> rollupSummaryScheduleItemsByParentId =
new Map<Id, List<LookupRollupSummaryScheduleItems__c>>();
for(LookupRollupSummaryScheduleItems__c rollupSummaryScheduleItem : rollupSummaryScheduleItems)
rollupSummaryScheduleItemsByParentId.put(rollupSummaryScheduleItem.ParentId__c, rollupSummaryScheduleItem);
{
List<LookupRollupSummaryScheduleItems__c> rollupsByParentId = rollupSummaryScheduleItemsByParentId.get(rollupSummaryScheduleItem.ParentId__c);
if(rollupsByParentId==null)
{
rollupsByParentId = new List<LookupRollupSummaryScheduleItems__c>();
rollupSummaryScheduleItemsByParentId.put(rollupSummaryScheduleItem.ParentId__c, rollupsByParentId);
}
rollupsByParentId.add(rollupSummaryScheduleItem);
}

// Update master records
List<LookupRollupSummaryLog__c> rollupSummaryLogs = new List<LookupRollupSummaryLog__c>();
Expand Down Expand Up @@ -353,7 +361,10 @@ global with sharing class RollupService
delete [select Id from LookupRollupSummaryLog__c where ParentId__c in :rollupSummaryScheduleItemsByParentId.keySet()];

// Delete any schedule items for successfully updated master records
delete rollupSummaryScheduleItemsByParentId.values();
List<LookupRollupSummaryScheduleItems__c> scheduleItemsToDelete = new List<LookupRollupSummaryScheduleItems__c>();
for(List<LookupRollupSummaryScheduleItems__c> scheduleItems : rollupSummaryScheduleItemsByParentId.values())
scheduleItemsToDelete.addAll(scheduleItems);
delete scheduleItemsToDelete;
}

/**
Expand Down Expand Up @@ -442,32 +453,32 @@ global with sharing class RollupService
// Process lookups,
// Realtime are added to a list for later LRE context creation and processing,
// Scheduled result in parent Id's being emitted to scheduled item object for later processing
List<LookupRollupSummary__c> realtimeLookups = new List<LookupRollupSummary__c>();
Map<Id, LookupRollupSummaryScheduleItems__c> scheduledItems = new Map<Id, LookupRollupSummaryScheduleItems__c>();
for(LookupRollupSummary__c lookup : lookups)
{
if(lookup.CalculationMode__c == RollupSummaries.CalculationMode.Realtime.name())
{
// Filter realtime looks in order to generate LRE contexts below
realtimeLookups.add(lookup);
}
else if(lookup.CalculationMode__c == RollupSummaries.CalculationMode.Scheduled.name())
{
// For scheduled rollups queue the parent Id record for processing
for (Id parentId : masterRecordIds)
{
LookupRollupSummaryScheduleItems__c scheduledItem = new LookupRollupSummaryScheduleItems__c();
scheduledItem.Name = parentId;
scheduledItem.LookupRollupSummary__c = lookup.Id;
scheduledItem.ParentId__c = parentId;
scheduledItem.QualifiedParentID__c = parentId + '#' + lookup.Id;
scheduledItems.put(parentId, scheduledItem);
}
}
}
// Add parent Id's to schedule items object
upsert scheduledItems.values() QualifiedParentID__c;
List<LookupRollupSummary__c> realtimeLookups = new List<LookupRollupSummary__c>();
List<LookupRollupSummaryScheduleItems__c> scheduledItems = new List<LookupRollupSummaryScheduleItems__c>();
for(LookupRollupSummary__c lookup : lookups)
{
if(lookup.CalculationMode__c == RollupSummaries.CalculationMode.Realtime.name())
{
// Filter realtime looks in order to generate LRE contexts below
realtimeLookups.add(lookup);
}
else if(lookup.CalculationMode__c == RollupSummaries.CalculationMode.Scheduled.name())
{
// For scheduled rollups queue the parent Id record for processing
for (Id parentId : masterRecordIds)
{
LookupRollupSummaryScheduleItems__c scheduledItem = new LookupRollupSummaryScheduleItems__c();
scheduledItem.Name = parentId;
scheduledItem.LookupRollupSummary__c = lookup.Id;
scheduledItem.ParentId__c = parentId;
scheduledItem.QualifiedParentID__c = parentId + '#' + lookup.Id;
scheduledItems.add(scheduledItem);
}
}
}

// Add parent Id's to schedule items object
upsert scheduledItems QualifiedParentID__c;

// Process each context (parent child relationship) and its associated rollups
Map<Id, SObject> masterRecords = new Map<Id, SObject>();
Expand Down
84 changes: 84 additions & 0 deletions rolluptool/src/classes/RollupServiceTest2.cls
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,90 @@ private with sharing class RollupServiceTest2
ACCOUNT_NUMBER_OF_LOCATIONS = accountFields.get('NumberOfLocations__c');
}

private testmethod static void testMultiRollupBothScheduledWithConditions()
{
// Test supported?
if(!TestContext.isSupported())
return;

// Test data
List<Decimal> rollups = new List<Decimal> { 250, 250, 50, 50 };

// Test data for rollup A
Decimal expectedResultA = 500;
RollupSummaries.AggregateOperation operationA = RollupSummaries.AggregateOperation.Sum;
String conditionA = 'Amount > 200';

// Test data for rollup B
Decimal expectedResultB = 2;
RollupSummaries.AggregateOperation operationB = RollupSummaries.AggregateOperation.Count;
String conditionB = 'Amount < 200';

// Configure rollup A
LookupRollupSummary__c rollupSummaryA = new LookupRollupSummary__c();
rollupSummaryA.Name = 'Total Opportunities greater than 200 into Annual Revenue on Account';
rollupSummaryA.ParentObject__c = 'Account';
rollupSummaryA.ChildObject__c = 'Opportunity';
rollupSummaryA.RelationShipField__c = 'AccountId';
rollupSummaryA.RelationShipCriteria__c = conditionA;
rollupSummaryA.FieldToAggregate__c = 'Amount';
rollupSummaryA.AggregateOperation__c = operationA.name();
rollupSummaryA.AggregateResultField__c = 'AnnualRevenue';
rollupSummaryA.Active__c = true;
rollupSummaryA.CalculationMode__c = 'Scheduled';

// Configure rollup B
LookupRollupSummary__c rollupSummaryB = new LookupRollupSummary__c();
rollupSummaryB.Name = 'Total Opportunities greater than 200 into Annual Revenue on Account';
rollupSummaryB.ParentObject__c = 'Account';
rollupSummaryB.ChildObject__c = 'Opportunity';
rollupSummaryB.RelationShipField__c = 'AccountId';
rollupSummaryB.RelationShipCriteria__c = conditionB;
rollupSummaryB.FieldToAggregate__c = 'CloseDate';
rollupSummaryB.AggregateOperation__c = operationB.name();
rollupSummaryB.AggregateResultField__c = 'NumberOfLocations__c';
rollupSummaryB.Active__c = true;
rollupSummaryB.CalculationMode__c = 'Scheduled';

// Insert rollup definitions
insert new List<LookupRollupSummary__c> { rollupSummaryA, rollupSummaryB };

// Test data
Account account = new Account();
account.Name = 'Test Account';
insert account;
List<Opportunity> opps = new List<Opportunity>();
for(Decimal rollupValue : rollups)
{
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.StageName = 'Open';
opp.CloseDate = System.today();
opp.AccountId = account.Id;
opp.Amount = rollupValue;
opps.add(opp);
}
insert opps;

// Assert rollup
Id accountId = account.Id;
Account accountResult = Database.query('select AnnualRevenue, NumberOfLocations__c from Account where Id = :accountId');
System.assertEquals(null, accountResult.AnnualRevenue); // This is a scheduled rollup so no realtime update
System.assertEquals(null, accountResult.get(ACCOUNT_NUMBER_OF_LOCATIONS)); // This is a scheduled rollup so no realtime update
System.assertEquals(2, [select Id from LookupRollupSummaryScheduleItems__c where ParentId__c = :account.Id].size()); // Assert a scheduled item has been created

// Run rollup job
Test.startTest();
RollupService.runJobToProcessScheduledItems();
Test.stopTest();

// Assert schedule items gone and rollup updated
accountResult = Database.query('select AnnualRevenue, NumberOfLocations__c from Account where Id = :accountId');
System.assertEquals(expectedResultA, accountResult.AnnualRevenue);
System.assertEquals(expectedResultB, accountResult.get(ACCOUNT_NUMBER_OF_LOCATIONS));
System.assertEquals(0, [select Id from LookupRollupSummaryScheduleItems__c where ParentId__c = :account.Id].size());
}

private testmethod static void testMultiRollupOneScheduled()
{
// Test supported?
Expand Down

0 comments on commit 43a8b39

Please sign in to comment.