Skip to content

Commit

Permalink
Patches RollupOrderBy__mdt issues when called from RollupFlowBulkProc…
Browse files Browse the repository at this point in the history
…essor
  • Loading branch information
jamessimone committed Oct 16, 2024
1 parent 1acc7d5 commit 7904ccf
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 9 deletions.
6 changes: 3 additions & 3 deletions extra-tests/classes/CustomMetadataDrivenTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private class CustomMetadataDrivenTests {
}
// uses a combination of Rollup__mdt found in extra-tests/customMetadata AND extra-tests/triggers/RollupChildTrigger.trigger
RollupParent__c parent = [SELECT Id FROM RollupParent__c];
// uses FIRST with TextField__c as the Order By field, MAX for the NumberField__c
// uses FIRST (with NumberField__c, TextField__c as the Order By fields), MAX for the NumberField__c
RollupChild__c childOne = new RollupChild__c(Name = 'Child one', TextField__c = 'a', RollupParent__c = parent.Id, NumberField__c = 3);
RollupChild__c childTwo = new RollupChild__c(Name = 'Child two', TextField__c = 'b', RollupParent__c = parent.Id, NumberField__c = 2);
insert new List<RollupChild__c>{ childOne, childTwo };
Expand Down Expand Up @@ -177,12 +177,12 @@ private class CustomMetadataDrivenTests {
RollupParent__c parent = [SELECT Id FROM RollupParent__c];

Test.startTest();
insertRollupChildrenAsync(parent.Id, 'a', 3);
insertRollupChildrenAsync(parent.Id, 'a', 1);
Test.stopTest();

parent = [SELECT Id, TextField__c, NumberField__c FROM RollupParent__c];
System.assertEquals('a', parent.TextField__c);
System.assertEquals(3, parent.NumberField__c);
System.assertEquals(2, parent.NumberField__c);
}

@Future
Expand Down
30 changes: 30 additions & 0 deletions extra-tests/classes/InvocableDrivenTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,34 @@ private class InvocableDrivenTests {
acc = [SELECT AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(child.PreferenceRank, acc.AnnualRevenue);
}

@IsTest
static void orderBysRetainRanking() {
RollupParent__c parent = new RollupParent__c(Name = 'Parent');
insert parent;

RollupChild__c childOne = new RollupChild__c(
RollupParent__c = parent.Id,
NumberField__c = 1,
TextField__c = 'b',
Id = RollupTestUtils.createId(RollupChild__c.SObjectType)
);
RollupChild__c childTwo = new RollupChild__c(
RollupParent__c = parent.Id,
NumberField__c = 1,
TextField__c = 'a',
Id = RollupTestUtils.createId(RollupChild__c.SObjectType)
);

Flow.Interview flowInterview = new Flow.Interview.Rollup_Integration_Rollup_Order_Bys(
new Map<String, Object>{ 'rollupChildren' => new List<RollupChild__c>{ childOne, childTwo } }
);

Test.startTest();
flowInterview.start();
Test.stopTest();

parent = [SELECT TextField__c FROM RollupParent__c WHERE Id = :parent.Id];
System.assertEquals(childTwo.TextField__c, parent.TextField__c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</values>
<values>
<field>Ranking__c</field>
<value xsi:type="xsd:double">1.0</value>
<value xsi:type="xsd:double">0.0</value>
</values>
<values>
<field>Rollup__c</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</values>
<values>
<field>Ranking__c</field>
<value xsi:type="xsd:double">0.0</value>
<value xsi:type="xsd:double">1.0</value>
</values>
<values>
<field>Rollup__c</field>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
<actionCalls>
<name>Call_CMDT_action</name>
<label>Call CMDT action</label>
<locationX>176</locationX>
<locationY>134</locationY>
<actionName>RollupFlowBulkProcessor</actionName>
<actionType>apex</actionType>
<dataTypeMappings>
<typeName>T__oldRecordsToRollup</typeName>
<typeValue>RollupChild__c</typeValue>
</dataTypeMappings>
<dataTypeMappings>
<typeName>T__recordsToRollup</typeName>
<typeValue>RollupChild__c</typeValue>
</dataTypeMappings>
<flowTransactionModel>CurrentTransaction</flowTransactionModel>
<inputParameters>
<name>deferProcessing</name>
<value>
<booleanValue>false</booleanValue>
</value>
</inputParameters>
<inputParameters>
<name>recordsToRollup</name>
<value>
<elementReference>rollupChildren</elementReference>
</value>
</inputParameters>
<inputParameters>
<name>rollupContext</name>
<value>
<stringValue>INSERT</stringValue>
</value>
</inputParameters>
<nameSegment>RollupFlowBulkProcessor</nameSegment>
<offset>0</offset>
<storeOutputAutomatically>true</storeOutputAutomatically>
<versionSegment>1</versionSegment>
</actionCalls>
<apiVersion>62.0</apiVersion>
<environments>Default</environments>
<interviewLabel>Rollup Integration - Rollup Order Bys {!$Flow.CurrentDateTime}</interviewLabel>
<label>Rollup Integration - Rollup Order Bys</label>
<processMetadataValues>
<name>BuilderType</name>
<value>
<stringValue>LightningFlowBuilder</stringValue>
</value>
</processMetadataValues>
<processMetadataValues>
<name>CanvasMode</name>
<value>
<stringValue>AUTO_LAYOUT_CANVAS</stringValue>
</value>
</processMetadataValues>
<processMetadataValues>
<name>OriginBuilderType</name>
<value>
<stringValue>LightningFlowBuilder</stringValue>
</value>
</processMetadataValues>
<processType>AutoLaunchedFlow</processType>
<start>
<locationX>50</locationX>
<locationY>0</locationY>
<connector>
<targetReference>Call_CMDT_action</targetReference>
</connector>
</start>
<status>Draft</status>
<variables>
<name>rollupChildren</name>
<dataType>SObject</dataType>
<isCollection>true</isCollection>
<isInput>true</isInput>
<isOutput>false</isOutput>
<objectType>RollupChild__c</objectType>
</variables>
</Flow>
3 changes: 2 additions & 1 deletion rollup/core/classes/RollupFlowBulkProcessor.cls
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ global without sharing class RollupFlowBulkProcessor {
// fixup order by children records
input.orderByFirstLast = flowInput.orderByFirstLast ?? '';
if (input.orderByFirstLast == '') {
input.orderByFirstLast = getConcatenatedOrderBys(meta.RollupOrderBys__r);
input.orderByFirstLast = getConcatenatedOrderBys(new List<RollupOrderBy__mdt>(meta.RollupOrderBys__r));
}
// metadata values that don't get overridden
input.currencyFieldMapping = meta.CurrencyFieldMapping__c;
Expand Down Expand Up @@ -152,6 +152,7 @@ global without sharing class RollupFlowBulkProcessor {
}

private static String getConcatenatedOrderBys(List<RollupOrderBy__mdt> orderBys) {
orderBys.sort(new RollupRepository.OrderBySorter());
List<String> joinedOrderBys = new List<String>();
for (RollupOrderBy__mdt orderBy : orderBys) {
String soloOrderBy = '';
Expand Down
6 changes: 3 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": "Fixes Rollup Order By sorting to always be deterministic, allows text templates in Full Recalc CMDT-driven Invocable invocable",
"versionNumber": "1.6.35.0",
"versionName": "Fixes Rollup Order By issue when called from RollupFlowBulkProcessor",
"versionNumber": "1.6.36.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 @@ -107,4 +107,4 @@
"apex-rollup@1.6.34": "04t6g000008OfJfAAK",
"apex-rollup@1.6.35": "04t6g000008OfKiAAK"
}
}
}

0 comments on commit 7904ccf

Please sign in to comment.