|
| 1 | +@isTest |
| 2 | +private class InvocableDrivenTests { |
| 3 | + // Driven by extra-tests/flows/Rollup_Integration_Multiple_Deferred_Case_Rollups.flow-meta.xml |
| 4 | + |
| 5 | + @TestSetup |
| 6 | + static void setup() { |
| 7 | + upsert new RollupSettings__c(IsEnabled__c = true); |
| 8 | + Account acc = new Account(Name = 'InvocableDrivenRollupTests'); |
| 9 | + insert acc; |
| 10 | + } |
| 11 | + |
| 12 | + @isTest |
| 13 | + static void shouldRollupMultipleDMLStatementsWithinSingleTransaction() { |
| 14 | + Account acc = [SELECT Id FROM Account]; |
| 15 | + Account reparentAccount = new Account(Name = 'Reparent'); |
| 16 | + insert reparentAccount; |
| 17 | + |
| 18 | + Date today = System.today(); |
| 19 | + |
| 20 | + Case one = new Case(Amount__c = 1, AccountId = acc.Id, Description = 'distinct', Subject = 'One', DateField__c = today.addDays(-2)); |
| 21 | + Case two = new Case(Amount__c = 2, AccountId = acc.Id, Description = 'again', Subject = 'Two', DateField__c = today); |
| 22 | + Case three = new Case(Amount__c = 0, AccountId = reparentAccount.Id, Description = 'something else', Subject = 'Three'); |
| 23 | + Case four = new Case(Amount__c = 0, AccountId = reparentAccount.Id, Description = one.Description, Subject = 'Four'); |
| 24 | + |
| 25 | + Test.startTest(); |
| 26 | + insert new List<Case>{ one, two, three, four }; |
| 27 | + |
| 28 | + one.Amount__c = 2; |
| 29 | + one.AccountId = reparentAccount.Id; |
| 30 | + update one; |
| 31 | + |
| 32 | + // Trigger recursive update after reparenting |
| 33 | + // this is important because it not only validates that the recursion |
| 34 | + // detection is working properly, but also because it validates that the |
| 35 | + // recursion detection is necessary to calculate the results properly! |
| 36 | + one.Subject = 'Z'; |
| 37 | + update one; |
| 38 | + Test.stopTest(); |
| 39 | + |
| 40 | + acc = [SELECT Id, Description, AnnualRevenue, Name, NumberOfEmployees, DateField__c FROM Account WHERE Id = :acc.Id]; |
| 41 | + reparentAccount = [SELECT Id, Description, AnnualRevenue, Name, NumberOfEmployees, DateField__c FROM Account WHERE Id = :reparentAccount.Id]; |
| 42 | + |
| 43 | + System.assertEquals(today, acc.DateField__c, 'LAST should have been updated to new last'); |
| 44 | + System.assertEquals(2, acc.AnnualRevenue, 'First account sum field should be decremented on reparent'); |
| 45 | + System.assertEquals(two.Description, acc.Description, 'CONCAT_DISTINCT should remove extra text on reparent'); |
| 46 | + System.assertEquals(1, acc.NumberOfEmployees); |
| 47 | + System.assertEquals(two.Subject, acc.Name); |
| 48 | + |
| 49 | + System.assertEquals(today.addDays(-2), reparentAccount.DateField__c); |
| 50 | + System.assertEquals(3, reparentAccount.NumberOfEmployees, 'Second account should properly reflect reparented record for number of employees'); |
| 51 | + System.assertEquals(one.Description + ', ' + three.Description, reparentAccount.Description, 'Second account should have only reparented case description'); |
| 52 | + System.assertEquals(one.Subject, reparentAccount.Name, 'Second account name field should reflect last subject'); |
| 53 | + System.assertEquals(2, reparentAccount.AnnualRevenue, 'Second account sum field should include updated amount'); |
| 54 | + } |
| 55 | +} |
0 commit comments