Skip to content

Commit

Permalink
v1.5.85 - COUNT bugfixes (#497)
Browse files Browse the repository at this point in the history
* Upgrading scanner dep

* Adding scan:graph script command

* Fixes an issue reported by @Windyo where count-based rollups going from one match to zero matches with a where clause on delete failed to decrement the parent value from 1 to 0

* Fixes a test that occasionally flaked due to an implicit reliance on Id ordering

* Fixes #496 by properly handling case where additional calc items are the only matches during a count full recalc update

* Updating version name

* Updating graph command (for now) to only scan rollup dir
  • Loading branch information
jamessimone authored Sep 18, 2023
1 parent d92f6f1 commit 4fb5d71
Show file tree
Hide file tree
Showing 10 changed files with 455 additions and 158 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=04t6g000008C6soAAC">
<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008C6zWAAS">
<img alt="Deploy to Salesforce"
src="./media/deploy-package-to-prod.png">
</a>

<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008C6soAAC">
<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008C6zWAAS">
<img alt="Deploy to Salesforce Sandbox"
src="./media/deploy-package-to-sandbox.png">
</a>
Expand Down
5 changes: 3 additions & 2 deletions extra-tests/classes/RollupIntegrationTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1603,8 +1603,8 @@ private class RollupIntegrationTests {
@IsTest
static void limitsCorrectlyOnUpdate() {
Account acc = [SELECT Id FROM Account];
ContactPointAddress cpa = new ContactPointAddress(ParentId = acc.Id, PreferenceRank = 50, Name = 'One');
ContactPointAddress two = new ContactPointAddress(ParentId = acc.Id, PreferenceRank = 25, Name = 'Two');
ContactPointAddress cpa = new ContactPointAddress(ParentId = acc.Id, PreferenceRank = 50, Name = 'A');
ContactPointAddress two = new ContactPointAddress(ParentId = acc.Id, PreferenceRank = 25, Name = 'B');
insert new List<ContactPointAddress>{ cpa, two };

Rollup.records = new List<ContactPointAddress>{ two };
Expand All @@ -1615,6 +1615,7 @@ private class RollupIntegrationTests {
Test.startTest();
Rollup.sumFromApex(ContactPointAddress.PreferenceRank, ContactPointAddress.ParentId, Account.Id, Account.AnnualRevenue, Account.SObjectType)
.addLimit(1, ContactPointAddress.PreferenceRank)
.addOrderBys(new List<RollupOrderBy__mdt>{ new RollupOrderBy__mdt(FieldName__c = 'Name', Ranking__c = 1) }, ContactPointAddress.PreferenceRank)
.runCalc();
Test.stopTest();

Expand Down
77 changes: 76 additions & 1 deletion extra-tests/classes/RollupTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,82 @@ private class RollupTests {

System.assertEquals(1, mock.Records.size(), 'Records should have been populated based on metadata AFTER_INSERT');
Account updatedAcc = (Account) mock.Records[0];
System.assertEquals(100100, updatedAcc.AnnualRevenue, 'SUM AFTER_INSERT should add the original cpaortunity amount to the default override based on CMDT');
System.assertEquals(100100, updatedAcc.AnnualRevenue, 'SUM AFTER_INSERT should add the original opp amount to the default override based on CMDT');
}

@IsTest
static void shouldOverrideNumberBasedDefaultBasedOnMetadataCount() {
Account acc = [SELECT Id, AnnualRevenue FROM Account];
Decimal originalValue = 1;
acc.AnnualRevenue = originalValue;
RollupAsyncProcessor.stubParentRecords = new List<SObject>{ acc };

ContactPointAddress cpa = new ContactPointAddress(ParentId = acc.Id, PreferenceRank = originalValue.intValue());
RollupTestUtils.DMLMock mock = RollupTestUtils.loadMock(new List<ContactPointAddress>{ cpa });
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupOperation__c = 'COUNT',
CalcItem__c = 'ContactPointAddress',
FullRecalculationDefaultNumberValue__c = 0,
CalcItemWhereClause__c = 'PreferenceRank != ' + cpa.PreferenceRank
)
};
Rollup.apexContext = TriggerOperation.BEFORE_DELETE;

Test.startTest();
Rollup.runFromTrigger();
Test.stopTest();

System.assertEquals(1, mock.Records.size(), 'Records should have been populated based on metadata BEFORE_DELETE');
Account updatedAcc = (Account) mock.Records[0];
System.assertEquals(0, updatedAcc.AnnualRevenue, 'COUNT BEFORE_DELETE should reset to the default override based on CMDT');
}

@IsTest
static void shouldNotDoubleCountOnDecrementForMatches() {
Account acc = [SELECT Id, AnnualRevenue FROM Account];
Decimal originalValue = 3;
acc.AnnualRevenue = originalValue;
RollupAsyncProcessor.stubParentRecords = new List<SObject>{ acc };

Opportunity opp = new Opportunity(AccountId = acc.Id, Amount = 100, Name = 'Will Not Match', CloseDate = System.today().addDays(2), StageName = 'A');
insert new List<Opportunity>{
new Opportunity(AccountId = acc.Id, Amount = 1, Name = 'First Match', CloseDate = System.today().addDays(-2), StageName = 'A'),
new Opportunity(AccountId = acc.Id, Amount = 2, Name = 'Second Match', CloseDate = System.today().addDays(-2), StageName = 'A'),
opp
};

RollupTestUtils.DMLMock mock = RollupTestUtils.loadMock(new List<Opportunity>{ opp });
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
RollupFieldOnCalcItem__c = 'Amount',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'AccountId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupOperation__c = 'COUNT',
CalcItem__c = 'Opportunity',
IsFullRecordSet__c = true,
CalcItemWhereClause__c = 'CloseDate <= TODAY'
)
};
Opportunity oldOpp = opp.clone(true);
oldOpp.CloseDate = System.today().addDays(-2);
Rollup.oldRecordsMap = new Map<Id, SObject>{ opp.Id => oldOpp };
Rollup.apexContext = TriggerOperation.AFTER_UPDATE;

Test.startTest();
Rollup.runFromTrigger();
Test.stopTest();

System.assertEquals(1, mock.Records.size(), 'Records should have been populated based on metadata AFTER_UPDATE');
Account updatedAcc = (Account) mock.Records[0];
System.assertEquals(2, updatedAcc.AnnualRevenue, 'COUNT AFTER_UPDATE should properly decrement when one item is updated to not match');
}

@IsTest
Expand Down
Loading

0 comments on commit 4fb5d71

Please sign in to comment.