-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRollupFlowFullRecalcTests.cls
587 lines (483 loc) · 28.7 KB
/
RollupFlowFullRecalcTests.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
@IsTest
private class RollupFlowFullRecalcTests {
@TestSetup
static void setup() {
Account acc = new Account(Name = RollupFlowFullRecalcTests.class.getName());
insert acc;
upsert new RollupSettings__c(IsEnabled__c = true);
}
@IsTest
static void shouldPerformFullRecalcWithGrandparentHierarchyFlow() {
Rollup.onlyUseMockMetadata = true; // necessary for namespaced package generation
Account acc = [SELECT Id FROM Account];
Account childAccount = new Account(Name = 'Hierarchy child', ParentId = acc.Id);
insert childAccount;
ParentApplication__c parentApp = new ParentApplication__c(Account__c = childAccount.Id, Name = 'Link to child account');
ParentApplication__c secondParentApp = new ParentApplication__c(Account__c = acc.Id, Name = 'Linked directly to parent account');
insert new List<ParentApplication__c>{ parentApp, secondParentApp };
// ensure that both a top-level child item linked only to the ultimate parent and one linked to the hierarchy child is included
List<Application__c> apps = new List<Application__c>{
new Application__c(Engagement_Score__c = 1000, ParentApplication__c = parentApp.Id, Name = 'Linked to Child Parent App'),
new Application__c(Engagement_Score__c = 1000, ParentApplication__c = secondParentApp.Id, Name = 'Linked to Direct Parent App')
};
insert apps;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(apps, 'REFRESH', 'SUM');
flowInputs[0].ultimateParentLookup = 'ParentId';
flowInputs[0].rollupToUltimateParent = true;
flowInputs[0].lookupFieldOnCalcItem = Application__c.ParentApplication__c.toString();
flowInputs[0].rollupFieldOnCalcItem = Application__c.Engagement_Score__c.toString();
flowInputs[0].grandparentRelationshipFieldPath = RollupTestUtils.getRelationshipPath(
new List<Schema.SObjectField>{ Application__c.ParentApplication__c, ParentApplication__c.Account__c, Account.AnnualRevenue }
);
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(2000, updatedAcc.AnnualRevenue, 'SUM REFRESH from flow should fully recalc');
}
@IsTest
static void shouldBulkifyBatchFullRecalcsProperly() {
RollupAsyncProcessor.additionalCalcItemCount = 1;
Rollup.defaultControl = new RollupControl__mdt(MaxLookupRowsBeforeBatching__c = 1, IsRollupLoggingEnabled__c = true);
Account acc = [SELECT Id FROM Account];
// ensure another matching item exists outside of the passed in list
insert new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One');
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ new ContactPointAddress(PreferenceRank = 1000, ParentId = acc.Id, Name = 'Two') };
insert cpas;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
Rollup.FlowInput secondFlowInput = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'CONCAT')[0];
secondFlowInput.rollupFieldOnOpObject = 'AccountNumber';
secondFlowInput.rollupFieldOnCalcItem = 'Name';
flowInputs.add(secondFlowInput);
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(2, flowOutputs.size(), 'Flow outputs were not provided');
for (Rollup.FlowOutput flowOutput : flowOutputs) {
System.assertEquals('SUCCESS', flowOutput.message);
System.assertEquals(true, flowOutput.isSuccess);
}
Account updatedAcc = [SELECT Id, AnnualRevenue, AccountNumber FROM Account];
System.assertEquals(1500, updatedAcc.AnnualRevenue, 'SUM REFRESH from flow should fully recalc');
System.assertEquals('One, Two', updatedAcc.AccountNumber);
}
@IsTest
static void shouldBulkifyQueueableFullRecalcsProperly() {
Account acc = [SELECT Id FROM Account];
// ensure another matching item exists outside of the passed in list
insert new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One');
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ new ContactPointAddress(PreferenceRank = 1000, ParentId = acc.Id, Name = 'Two') };
insert cpas;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
Rollup.FlowInput secondFlowInput = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'CONCAT')[0];
secondFlowInput.rollupFieldOnOpObject = 'AccountNumber';
secondFlowInput.rollupFieldOnCalcItem = 'Name';
flowInputs.add(secondFlowInput);
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(2, flowOutputs.size(), 'Flow outputs were not provided');
for (Rollup.FlowOutput flowOutput : flowOutputs) {
System.assertEquals('SUCCESS', flowOutput.message);
System.assertEquals(true, flowOutput.isSuccess);
}
Account updatedAcc = [SELECT Id, AnnualRevenue, AccountNumber FROM Account];
System.assertEquals(1500, updatedAcc.AnnualRevenue, 'SUM REFRESH from flow should fully recalc');
System.assertEquals('One, Two', updatedAcc.AccountNumber);
}
@IsTest
static void shouldPerformFullRecalcFromFlowChildrenWithReparenting() {
Account acc = [SELECT Id FROM Account];
Account reparentedAccount = new Account(Name = 'Reparented Refresh', AnnualRevenue = 1000);
insert reparentedAccount;
insert new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One');
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = reparentedAccount.AnnualRevenue.intValue(), ParentId = reparentedAccount.Id, Name = 'Two')
};
insert cpas;
reparentedAccount = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :reparentedAccount.Id];
System.assertEquals(cpas[0].PreferenceRank, reparentedAccount.AnnualRevenue, 'Reparenting test set up under wrong conditions!');
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
ContactPointAddress clonedCpa = cpas[0].clone(true);
flowInputs[0].oldRecordsToRollup = new List<ContactPointAddress>{ clonedCpa };
cpas[0].ParentId = acc.Id;
flowInputs[0].recordsToRollup = cpas;
update cpas;
Test.startTest();
Rollup.performRollup(flowInputs);
Test.stopTest();
acc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(1500, acc.AnnualRevenue, 'SUM REFRESH from flow should fully recalc');
reparentedAccount = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :reparentedAccount.Id];
System.assertEquals(null, reparentedAccount.AnnualRevenue, 'Reparenting with REFRESH should run decrement logic on old parent');
}
@IsTest
static void shouldPerformFullRecalcFromFlowParent() {
List<Account> accs = [SELECT Id, AnnualRevenue FROM Account];
insert new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = 500, ParentId = accs[0].Id, Name = 'One'),
new ContactPointAddress(PreferenceRank = 1000, ParentId = accs[0].Id, Name = 'Two')
};
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(accs, 'REFRESH', 'SUM');
flowInputs[0].isRollupStartedFromParent = true;
flowInputs[0].calcItemTypeWhenRollupStartedFromParent = 'ContactPointAddress';
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account];
System.assertEquals(1500, updatedAcc.AnnualRevenue, 'SUM REFRESH from flow started from parent should fully recalc');
}
@IsTest
static void clearsParentValuesWhenNoRefreshMatches() {
Account acc = (Account) RollupTestUtils.queryRecord(
Schema.Account.SObjectType,
new List<Schema.SObjectField>{ Account.AnnualRevenue, Account.Description }
);
acc.AnnualRevenue = 1500;
acc.Description = 'This is the value that should be persisted';
update acc;
// simulate un-related, in-flight update
acc.Description = 'Something else';
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(new List<Account>{ acc }, 'REFRESH', 'SUM');
flowInputs[0].isRollupStartedFromParent = true;
flowInputs[0].calcItemTypeWhenRollupStartedFromParent = 'ContactPointAddress';
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue, Description FROM Account];
System.assertEquals(null, updatedAcc.AnnualRevenue, 'SUM REFRESH from flow started from parent should fully clear');
System.assertEquals('This is the value that should be persisted', updatedAcc.Description, 'Unrelated field should not be cleared');
}
@IsTest
static void clearsParentValuesWhenNoUpdateMatches() {
Account acc = (Account) RollupTestUtils.queryRecord(Schema.Account.SObjectType, new List<Schema.SObjectField>{ Account.AnnualRevenue });
acc.AnnualRevenue = 1500;
update acc;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(new List<Account>{ acc }, 'UPSERT', 'SUM');
flowInputs[0].isRollupStartedFromParent = true;
flowInputs[0].calcItemTypeWhenRollupStartedFromParent = 'ContactPointAddress';
flowInputs[0].oldRecordsToRollup = new List<SObject>{ null };
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account];
System.assertEquals(null, updatedAcc.AnnualRevenue, 'SUM from flow started from parent should fully clear');
}
@IsTest
static void properlyPerformsFullRecalcsOnUpdateFromParent() {
List<Account> accs = [SELECT Id, AnnualRevenue FROM Account];
insert new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = 500, ParentId = accs[0].Id, Name = 'One'),
new ContactPointAddress(PreferenceRank = 1000, ParentId = accs[0].Id, Name = 'Two')
};
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(accs, 'UPDATE', 'COUNT');
flowInputs[0].isRollupStartedFromParent = true;
flowInputs[0].calcItemTypeWhenRollupStartedFromParent = 'ContactPointAddress';
flowInputs[0].oldRecordsToRollup = accs;
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account];
System.assertEquals(2, updatedAcc.AnnualRevenue, 'COUNT UPDATE from flow started from parent should fully recalc');
}
@IsTest
static void shouldFilterOnParentFieldsProperlyWhenInvokedFromParent() {
Account acc = [SELECT Id, AnnualRevenue FROM Account];
System.assertEquals(null, acc.AnnualRevenue, 'Test has started under wrong conditions');
insert new Contact(LastName = 'Parent field test', AccountId = acc.Id);
List<Account> accs = new List<Account>{ acc };
RollupTestUtils.DMLMock mock = RollupTestUtils.loadMock(accs);
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(accs, 'REFRESH', 'COUNT');
flowInputs[0].lookupFieldOnCalcItem = 'AccountId';
flowInputs[0].lookupFieldOnOpObject = 'Id';
flowInputs[0].rollupFieldOnCalcItem = 'Id';
flowInputs[0].isRollupStartedFromParent = true;
flowInputs[0].calcItemTypeWhenRollupStartedFromParent = 'Contact';
flowInputs[0].oldRecordsToRollup = new List<Account>{ new Account(Id = acc.Id, AnnualRevenue = 250) };
flowInputs[0].calcItemWhereClause = 'Account.AnnualRevenue != 250';
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
System.assertEquals(1, mock.Records.size(), 'REFRESH_COUNT from flow did not update account');
Account updatedAcc = (Account) mock.Records[0];
System.assertEquals(1, updatedAcc.AnnualRevenue, 'COUNT did not refresh successfully');
}
@IsTest
static void shouldAllowRollupFromParentByInvocable() {
Account acc = [SELECT Id, AnnualRevenue FROM Account];
ContactPointAddress cpa = new ContactPointAddress(ParentId = acc.Id, Name = 'rollup from parent invocable', PreferenceRank = -50);
insert cpa;
List<Account> accs = new List<Account>{ acc };
RollupTestUtils.DMLMock mock = RollupTestUtils.loadMock(accs);
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(accs, 'INSERT', 'SUM');
flowInputs[0].isRollupStartedFromParent = true;
flowInputs[0].calcItemTypeWhenRollupStartedFromParent = 'ContactPointAddress';
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
System.assertEquals(1, mock.Records.size(), 'SUM AFTER_INSERT from flow did not update accounts');
Account updatedAcc = (Account) mock.Records[0];
System.assertEquals(cpa.PreferenceRank, updatedAcc.AnnualRevenue, 'SUM AFTER_INSERT from flow should match input PreferenceRank');
}
@IsTest
static void multipleIsFullRecordSetsProperlyClearParentFieldsOnce() {
Account acc = [SELECT Id, AnnualRevenue FROM Account];
ContactPointAddress one = new ContactPointAddress(ParentId = acc.Id, Name = 'child one', PreferenceRank = 50);
ContactPointAddress two = new ContactPointAddress(ParentId = acc.Id, Name = 'child two', PreferenceRank = 50);
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ one, two };
insert cpas;
cpas.remove(1);
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'INSERT', 'SUM');
flowInputs[0].isFullRecordSet = true;
Rollup.FlowInput secondFullRecalc = RollupTestUtils.prepareFlowTest(cpas, 'INSERT', 'COUNT')[0];
secondFullRecalc.isFullRecordSet = true;
secondFullRecalc.rollupFieldOnCalcItem = 'Name';
secondFullRecalc.rollupFieldOnOpObject = 'NumberOfEmployees';
flowInputs.add(secondFullRecalc);
Test.startTest();
Rollup.performRollup(flowInputs);
for (Rollup.FlowInput flowInput : flowInputs) {
flowInput.recordsToRollup = new List<SObject>{ two };
flowInput.shouldRunSync = true;
}
// same operations, different set of inputs shouldn't add to existing count/sum; they should be reset
Rollup.performRollup(flowInputs);
Test.stopTest();
acc = [SELECT AnnualRevenue, NumberOfEmployees FROM Account];
System.assertEquals(one.PreferenceRank + two.PreferenceRank, acc.AnnualRevenue);
System.assertEquals(2, acc.NumberOfEmployees);
}
@IsTest
static void shouldPerformFullRecalcFromFlowChildren() {
Rollup.defaultControl = new RollupControl__mdt(ShouldAbortRun__c = true);
Account acc = [SELECT Id FROM Account];
Account secondParent = new Account(Name = 'Second', AnnualRevenue = 5);
insert secondParent;
insert new List<ContactPointAddress>{
// ensure another matching item exists outside of the passed in list
new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One'),
// ensure that the PreferenceRank associated with the second parent specifically does NOT match
// the second parent's existing annual revenue; we want to validate that a constrained full recalc only
// updates the parents of the records that we've passed in
new ContactPointAddress(PreferenceRank = secondParent.AnnualRevenue.intValue() * 2, ParentId = secondParent.Id, Name = 'Two')
};
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ new ContactPointAddress(PreferenceRank = 1000, ParentId = acc.Id, Name = 'First child') };
insert cpas;
Rollup.shouldRefreshCachedControl = true;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
Rollup.FlowInput secondInput = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'COUNT')[0];
secondInput.rollupFieldOnCalcItem = 'Id';
secondInput.rollupFieldOnOpObject = 'NumberOfEmployees';
secondInput.rollupOperation = 'COUNT';
secondInput.calcItemWhereClause = 'PreferenceRank > 0';
flowInputs.add(secondInput);
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(2, flowOutputs.size(), 'Incorrect flow outputs were provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(1500, updatedAcc.AnnualRevenue, 'SUM REFRESH from flow should fully recalc');
Account updatedSecondParent = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :secondParent.Id];
System.assertEquals(secondParent.AnnualRevenue, updatedSecondParent.AnnualRevenue, 'Out of scope parent values should not have been affected');
}
@IsTest
static void shouldRefreshMultipleBatchFullRecalcs() {
Rollup.defaultControl = new RollupControl__mdt(MaxLookupRowsBeforeBatching__c = 0, IsRollupLoggingEnabled__c = true, MaxRollupRetries__c = 3);
Account acc = [SELECT Id FROM Account];
Individual secondParent = new Individual(LastName = 'Second');
insert secondParent;
List<ContactPointAddress> childrenToInsert = new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'A1'),
new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'A2'),
new ContactPointAddress(PreferenceRank = 250, ParentId = secondParent.Id, Name = 'B1'),
new ContactPointAddress(PreferenceRank = 250, ParentId = secondParent.Id, Name = 'B2')
};
insert childrenToInsert;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ childrenToInsert[0], childrenToInsert[2] };
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
flowInputs[0].deferProcessing = true;
flowInputs[0].calcItemWhereClause = 'PreferenceRank > 0';
List<Rollup.FlowInput> secondInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
secondInputs[0].rollupSObjectName = 'Individual';
secondInputs[0].rollupFieldOnOpObject = 'ConsumerCreditScore';
secondInputs[0].rollupOperation = 'SUM';
secondInputs[0].deferProcessing = true;
Test.startTest();
Rollup.performRollup(flowInputs);
Rollup.performRollup(secondInputs);
Rollup.processStoredFlowRollups();
Test.stopTest();
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(1000, updatedAcc.AnnualRevenue);
Individual updatedSecondParent = [SELECT Id, ConsumerCreditScore FROM Individual WHERE Id = :secondParent.Id];
System.assertEquals(500, updatedSecondParent.ConsumerCreditScore);
}
@IsTest
static void shouldCorrectlyFilterParentFieldsFromFlowChildren() {
Account acc = [SELECT Id, Name FROM Account];
// ensure another matching item exists outside of the passed in list
insert new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One');
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ new ContactPointAddress(PreferenceRank = 1000, ParentId = acc.Id, Name = 'Two') };
insert cpas;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
flowInputs[0].calcItemWhereClause = 'Parent.Name = \'' + acc.Name + '\'';
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account];
System.assertEquals(1500, updatedAcc.AnnualRevenue, 'Child object where clause with parent filtering should work for REFRESH');
}
@IsTest
static void shouldDecrementProperlyOnRefreshUpdateWithParentFilterFields() {
Account acc = [SELECT Id, Name FROM Account];
Account second = new Account(Name = 'Second', AnnualRevenue = 1000);
insert second;
// ensure another matching item exists outside of the passed in list
insert new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One');
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = second.AnnualRevenue.intValue(), ParentId = second.Id, Name = 'Two')
};
insert cpas;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
flowInputs[0].calcItemWhereClause = 'Parent.Name = \'' + acc.Name + '\'';
flowInputs[0].oldRecordsToRollup = new List<SObject>{
new ContactPointAddress(PreferenceRank = cpas[0].PreferenceRank, ParentId = acc.Id, Name = 'Two', Id = cpas[0].Id)
};
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(500, updatedAcc.AnnualRevenue, 'Child object where clause with parent filtering should work for REFRESH');
Account secondUpdatedAcc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :second.Id];
System.assertEquals(null, secondUpdatedAcc.AnnualRevenue, 'Amount should have decremented on update!');
}
@IsTest
static void shouldPerformFullRecalcWithHierarchy() {
Account acc = [SELECT Id FROM Account];
Account childAccount = new Account(Name = 'Hierarchy child', ParentId = acc.Id);
insert childAccount;
insert new ContactPointAddress(PreferenceRank = 500, ParentId = childAccount.Id, Name = 'One');
// ensure that a top-level child item linked only to the ultimate parent is included
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ new ContactPointAddress(PreferenceRank = 1000, ParentId = acc.Id, Name = 'Two') };
insert cpas;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
flowInputs[0].ultimateParentLookup = 'ParentId';
flowInputs[0].rollupToUltimateParent = true;
Test.startTest();
List<Rollup.FlowOutput> flowOutputs = Rollup.performRollup(flowInputs);
Test.stopTest();
System.assertEquals(1, flowOutputs.size(), 'Flow outputs were not provided');
System.assertEquals('SUCCESS', flowOutputs[0].message);
System.assertEquals(true, flowOutputs[0].isSuccess);
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(1500, updatedAcc.AnnualRevenue, 'SUM REFRESH from flow should fully recalc');
}
@IsTest
static void multipleHierarchyRollupsSuccessfullyFullRecalc() {
Account acc = [SELECT Id FROM Account];
Account childAccount = new Account(Name = 'Hierarchy child', ParentId = acc.Id);
insert childAccount;
insert new ContactPointAddress(PreferenceRank = 500, ParentId = childAccount.Id, Name = 'One');
// ensure that a top-level child item linked only to the ultimate parent is included
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ new ContactPointAddress(PreferenceRank = 1000, ParentId = acc.Id, Name = 'Two') };
insert cpas;
List<Rollup.FlowInput> flowInputs = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM');
Rollup.FlowInput secondInput = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'CONCAT_DISTINCT')[0];
secondInput.rollupFieldOnOpObject = 'Name';
secondInput.rollupFieldOnCalcItem = 'Name';
flowInputs.add(secondInput);
for (Rollup.FlowInput flowInput : flowInputs) {
flowInput.ultimateParentLookup = 'ParentId';
flowInput.rollupToUltimateParent = true;
}
Test.startTest();
Rollup.performRollup(flowInputs);
Test.stopTest();
Account updatedAcc = [SELECT Id, AnnualRevenue, Name FROM Account WHERE Id = :acc.Id];
System.assertEquals(1500, updatedAcc.AnnualRevenue, 'SUM REFRESH hierarchy from flow should fully recalc');
System.assertEquals('One, Two', updatedAcc.Name, 'CONCAT_DISTINCT REFRESH hierarchy from flow should full recalc');
}
@IsTest
static void shouldCorrectlyFindAdjacentChildrenForRollupsToMultipleParents() {
List<Account> accounts = new List<Account>{
new Account(Name = 'Parent 1'),
new Account(Name = 'Parent 2'),
new Account(Name = 'Parent 3'),
new Account(Name = 'Parent 4')
};
insert accounts;
Individual bParentOne = new Individual(LastName = 'B1', ConsumerCreditScore = 3, ConvictionsCount = 2);
Individual bParentTwo = new Individual(LastName = 'B2', ConsumerCreditScore = 7, ConvictionsCount = 2);
insert new List<Individual>{ bParentOne, bParentTwo };
List<ContactPointAddress> childrenToInsert = new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = 1, ParentId = accounts[0].Id, Name = bParentOne.Id),
new ContactPointAddress(PreferenceRank = 2, ParentId = accounts[1].Id, Name = bParentOne.Id),
new ContactPointAddress(PreferenceRank = 3, ParentId = accounts[2].Id, Name = bParentTwo.Id),
new ContactPointAddress(PreferenceRank = 4, ParentId = accounts[3].Id, Name = bParentTwo.Id)
};
insert childrenToInsert;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ childrenToInsert[0], childrenToInsert[2] };
Rollup.FlowInput firstInput = RollupTestUtils.prepareFlowTest(cpas, 'REFRESH', 'SUM')[0];
firstInput.deferProcessing = true;
Rollup.FlowInput secondInput = firstInput.clone();
secondInput.lookupFieldOnCalcItem = 'Name';
secondInput.rollupSObjectName = 'Individual';
secondInput.rollupFieldOnOpObject = 'ConsumerCreditScore';
Rollup.FlowInput thirdInput = secondInput.clone();
thirdInput.calcItemWhereClause = 'PreferenceRank > 0';
thirdInput.rollupFieldOnOpObject = 'ConvictionsCount';
thirdInput.rollupOperation = 'COUNT';
Rollup.FlowInput fourthInput = secondInput.clone();
fourthInput.rollupSObjectName = 'Task';
fourthInput.rollupFieldOnOpObject = 'CallDurationInSeconds';
fourthInput.calcItemWhereClause = '(NOT Name LIKE \'' + bParentOne.Id.toString().substring(0, 3) + '%\')';
Test.startTest();
Rollup.performRollup(new List<Rollup.FlowInput>{ firstInput, secondInput, thirdInput, fourthInput });
Rollup.processStoredFlowRollups();
Test.stopTest();
Map<Id, Decimal> accountToExpectedAmount = new Map<Id, Decimal>{
accounts[0].Id => childrenToInsert[0].PreferenceRank,
accounts[1].Id => childrenToInsert[1].PreferenceRank,
accounts[2].Id => childrenToInsert[2].PreferenceRank,
accounts[3].Id => childrenToInsert[3].PreferenceRank
};
for (Account updatedAcc : [SELECT Id, Name, AnnualRevenue FROM Account WHERE Id = :accounts]) {
System.assertEquals(accountToExpectedAmount.get(updatedAcc.Id), updatedAcc.AnnualRevenue, updatedAcc);
}
bParentOne = [SELECT Id, ConsumerCreditScore, ConvictionsCount FROM Individual WHERE Id = :bParentOne.Id];
System.assertEquals(3, bParentOne.ConsumerCreditScore);
System.assertEquals(2, bParentOne.ConvictionsCount);
bParentTwo = [SELECT Id, ConsumerCreditScore, ConvictionsCount FROM Individual WHERE Id = :bParentTwo.Id];
System.assertEquals(7, bParentTwo.ConsumerCreditScore);
System.assertEquals(2, bParentTwo.ConvictionsCount);
}
}