Skip to content

Commit

Permalink
v1.6.22 - Grandparent COUNT_DISTINCT deletion bugfix (#582)
Browse files Browse the repository at this point in the history
* Fixes #581 by properly handling COUNT_DISTINCT for grandparent rollup deletions
  • Loading branch information
jamessimone committed Apr 16, 2024
1 parent 936c135 commit b7cafd8
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ As well, don't miss [the Wiki](../../wiki), which includes even more info for co

## Deployment & Setup

<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008OatsAAC">
<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008OavAAAS">
<img alt="Deploy to Salesforce"
src="./media/deploy-package-to-prod.png">
</a>

<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008OatsAAC">
<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008OavAAAS">
<img alt="Deploy to Salesforce Sandbox"
src="./media/deploy-package-to-sandbox.png">
</a>
Expand Down
47 changes: 47 additions & 0 deletions extra-tests/classes/RollupIntegrationTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,53 @@ private class RollupIntegrationTests {
System.assertEquals(greatGrandparent.Name, updatedGreatGrandparent.Name, 'Great-grandparent name should not have been appended based on exclusions');
}

@IsTest
static void shouldCountDistinctForGrandparentDeletes() {
if (RollupTestUtils.IS_NAMESPACED_PACKAGE_ORG) {
return;
}

Rollup.onlyUseMockMetadata = true;
Rollup.shouldFlattenAsyncProcesses = true;
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = ApplicationLog__c.SObjectType.getDescribe(SObjectDescribeOptions.DEFERRED).getName(),
RollupFieldOnCalcItem__c = 'Name',
LookupFieldOnCalcItem__c = ApplicationLog__c.Application__c.getDescribe().getName(),
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupOperation__c = 'COUNT_DISTINCT',
GrandparentRelationshipFieldPath__c = RollupTestUtils.getRelationshipPath(
new List<Schema.SObjectField>{ ApplicationLog__c.Application__c, Application__c.ParentApplication__c, ParentApplication__c.Account__c, Account.Name }
)
)
};

Account greatGrandparent = new Account(Name = 'Great-grandparent 1');
insert greatGrandparent;

ParentApplication__c grandParent = new ParentApplication__c(Name = 'Grandparent 1', Account__c = greatGrandparent.Id);
ParentApplication__c secondGrandparent = new ParentApplication__c(Name = 'Grandparent 2', Account__c = greatGrandparent.Id);
List<ParentApplication__c> parentApps = new List<ParentApplication__c>{ grandParent, secondGrandparent };
insert parentApps;

Application__c parent = new Application__c(Name = 'Parent 1', ParentApplication__c = grandParent.Id);
Application__c secondParent = new Application__c(Name = 'Parent 2', ParentApplication__c = secondGrandparent.Id);
insert new List<Application__c>{ parent, secondParent };

ApplicationLog__c child = new ApplicationLog__c(Application__c = secondParent.Id, Name = 'Will Still Exist');
ApplicationLog__c secondChild = new ApplicationLog__c(Name = 'To be deleted', Application__c = parent.Id);
insert new List<ApplicationLog__c>{ child, secondChild };

Test.startTest();
delete secondChild;
Test.stopTest();

greatGrandparent = [SELECT AnnualRevenue FROM Account WHERE Id = :greatGrandparent.Id];
Assert.areEqual(1, greatGrandparent.AnnualRevenue, 'Should still count non-deleted great-grandchild');
}

@IsTest
static void shouldProperlyFilterPolymorphicWhatFields() {
Account acc = new Account(Name = 'Matching type');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apex-rollup",
"version": "1.6.21",
"version": "1.6.22",
"description": "Fast, configurable, elastically scaling custom rollup solution. Apex Invocable action, one-liner Apex trigger/CMDT-driven logic, and scheduled Apex-ready.",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions rollup-namespaced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ For more info, see the base `README`.

## Deployment & Setup

<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008OatxAAC">
<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008OavFAAS">
<img alt="Deploy to Salesforce"
src="./media/deploy-package-to-prod.png">
</a>

<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008OatxAAC">
<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008OavFAAS">
<img alt="Deploy to Salesforce Sandbox"
src="./media/deploy-package-to-sandbox.png">
</a>
2 changes: 1 addition & 1 deletion rollup-namespaced/sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"apex-rollup-namespaced@1.1.13": "04t6g000008OaYwAAK",
"apex-rollup-namespaced@1.1.14": "04t6g000008OaZBAA0",
"apex-rollup-namespaced@1.1.16": "04t6g000008OaZaAAK",
"apex-rollup-namespaced@1.1.17": "04t6g000008OatxAAC"
"apex-rollup-namespaced@1.1.17": "04t6g000008OavFAAS"
}
}
4 changes: 3 additions & 1 deletion rollup/core/classes/RollupAsyncProcessor.cls
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,9 @@ global virtual without sharing class RollupAsyncProcessor extends Rollup impleme
// calling clear in a loop here might look interesting - and it would be a massive problem if not for the
// bag only responding to the very first clear() invocation. everything after that is a no-op (so, any rollup with more
// than one rollup operation going at a time)
if (rollup.triggerContext == System.TriggerOperation.BEFORE_DELETE || rollup.op.name().contains('DELETE')) {
if (
(rollup.triggerContext == System.TriggerOperation.BEFORE_DELETE || rollup.op.name().contains('DELETE')) && rollup.traversal?.getIsFinished() != true
) {
bag.clear();
}
}
Expand Down
2 changes: 1 addition & 1 deletion rollup/core/classes/RollupLogger.cls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global without sharing virtual class RollupLogger implements ILogger {
@TestVisible
// this gets updated via the pipeline as the version number gets incremented
private static final String CURRENT_VERSION_NUMBER = 'v1.6.21';
private static final String CURRENT_VERSION_NUMBER = 'v1.6.22';
private static final System.LoggingLevel FALLBACK_LOGGING_LEVEL = System.LoggingLevel.DEBUG;
private static final RollupPlugin PLUGIN = new RollupPlugin();

Expand Down
3 changes: 3 additions & 0 deletions rollup/core/classes/RollupRelationshipFieldFinder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,9 @@ public without sharing class RollupRelationshipFieldFinder {
.setArg(inclusiveIds)
.setArg(bindVar, this.records)
.get();
if (this.metadata.RollupOperation__c.contains('DELETE')) {
this.records.clear();
}
this.records.addAll(additionalCalcItems);
return additionalCalcItems;
} catch (Exception ex) {
Expand Down
7 changes: 4 additions & 3 deletions sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"package": "apex-rollup",
"path": "rollup",
"scopeProfiles": true,
"versionName": "Updating to null coalesce operator when applicable",
"versionNumber": "1.6.21.0",
"versionName": "Fixes grandparent rollup logic for deletes with COUNT_DISTINCT",
"versionNumber": "1.6.22.0",
"versionDescription": "Fast, configurable, elastically scaling custom rollup solution. Apex Invocable action, one-liner Apex trigger/CMDT-driven logic, and scheduled Apex-ready.",
"releaseNotesUrl": "https://github.com/jamessimone/apex-rollup/releases/latest",
"unpackagedMetadata": {
Expand Down Expand Up @@ -104,6 +104,7 @@
"apex-rollup@1.6.18": "04t6g000008Oak1AAC",
"apex-rollup@1.6.19": "04t6g000008Oal4AAC",
"apex-rollup@1.6.20": "04t6g000008OanPAAS",
"apex-rollup@1.6.21": "04t6g000008OatsAAC"
"apex-rollup@1.6.21": "04t6g000008OatsAAC",
"apex-rollup@1.6.22": "04t6g000008OavAAAS"
}
}

0 comments on commit b7cafd8

Please sign in to comment.