SFDO-Community/declarative-lookup-rollup-summaries

After Delete trigger for merging fails when new dlrs added

yahirah opened this issue · 2 comments

Hi,
I have (had) following problem: I have few dlrs defined on Opportunity to calculate some stuff for parent Accounts, with support for merging Accounts turn on. Recently I added two new dlrs for the same relation and while merging clients I received:

Apex trigger dlrs_AccountTrigger caused an unexpected exception, contact your administrator: dlrs_AccountTrigger: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Class.dlrs.RollupService.updateMasterRollupsTrigger: line 938, column 1

I looked at the dlrs code a little bit and I think I can understand what happened:

  1. if(shadowRollupsToInsertByRollupId.size()>0)
  2. {
  3. // Insert new shadow rollups and associate with the correct schedule item`
  4. insert shadowRollupsToInsertByRollupId.values();
  5. for(LookupRollupSummaryScheduleItems__c scheduledItem : scheduledItemsForMDTs) {
  6.      String rollupId = scheduledItem.LookupRollupSummary2__c;
    
  7.      scheduledItem.LookupRollupSummary__c = shadowRollupsToInsertByRollupId.get(rollupId).Id;
    
  8. }
  9. }

While receiving an error, I only have shadow lookup for 6 out of 8 dlrs for this relations (because two new ones never experienced merge). RollupService is try to insert two new rollups in line 935 (4) (which with he probably succeeds), but then in lines 936-939 (5-7) it iterates over ALL 8 items, while it only have data for two of them in the map, which means the map returns null for anything different than the two ones that were inserted and, bang, null pointer exception when asking for Id of the null.

*I fixed this error on production * with removing all shadow lookups (my dlrs is defined in custom metadata).

I think it can be easily fixed with one null check in the code. I can do that, but wanted to confirm that the code is doing what I think is doing ;)

Thank you @yahirah for this analysis, i'm literally about to sit down and look at this bug, that has also been reported on other issues and in the Chatter group. Most useful, thank you!

Fixed in v2.5.