From 023ae41f0219e8c82f589556862a57d5fff5b4eb Mon Sep 17 00:00:00 2001 From: Jon Davis Date: Thu, 20 Aug 2015 00:46:22 -0700 Subject: [PATCH] Issue #229 - Ensure context is shared when rollups differ by case only Additional debug statements have been removed from tests in this commit. --- rolluptool/src/classes/RollupService.cls | 7 +- rolluptool/src/classes/RollupServiceTest4.cls | 35 ------- rolluptool/src/classes/RollupSummaries.cls | 91 +++++++++++++++++++ 3 files changed, 97 insertions(+), 36 deletions(-) diff --git a/rolluptool/src/classes/RollupService.cls b/rolluptool/src/classes/RollupService.cls index 056a134d..3d710851 100644 --- a/rolluptool/src/classes/RollupService.cls +++ b/rolluptool/src/classes/RollupService.cls @@ -696,7 +696,12 @@ global with sharing class RollupService // Determine if an LREngine Context has been created for this parent child relationship, filter combination or underlying query type and sharing mode? String rsfType = rsf.isAggregateBasedRollup() ? 'aggregate' : 'query'; String orderBy = String.isBlank(Lookup.FieldToOrderBy__c) ? '' : Lookup.FieldToOrderBy__c; - String contextKey = lookup.ParentObject__c + '#' + lookup.RelationshipField__c + '#' + lookup.RelationShipCriteria__c + '#' + rsfType + '#' + sharingMode + '#' + orderBy; + // Lowering case on Describable fields is only required for Legacy purposes since LookupRollupSummary__c records + // will be updated with describe names on insert/update moving forward. + // Ideally this would not be needed to save CPU cycles but including to ensure context is properly re-used when possible for + // rollups that have not been updated/inserted after the insert/update enhancement is applied + // Unable to lower RelationShipCriteria__c because of field value case-(in)sensitivity configuration + String contextKey = lookup.ParentObject__c.toLowerCase() + '#' + lookup.RelationshipField__c.toLowerCase() + '#' + lookup.RelationShipCriteria__c + '#' + rsfType + '#' + sharingMode + '#' + orderBy.toLowerCase(); LREngine.Context lreContext = engineCtxByParentRelationship.get(contextKey); if(lreContext==null) diff --git a/rolluptool/src/classes/RollupServiceTest4.cls b/rolluptool/src/classes/RollupServiceTest4.cls index 3bab5614..924c5786 100644 --- a/rolluptool/src/classes/RollupServiceTest4.cls +++ b/rolluptool/src/classes/RollupServiceTest4.cls @@ -236,11 +236,6 @@ private class RollupServiceTest4 { insert children; // Assert limits - // TODO: Remove debug statements - in-place to get full picture when test fails - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) })); - // + One query on Rollup object // + One query on LookupChild__c for rollup System.assertEquals(beforeQueries + 2, Limits.getQueries()); @@ -363,11 +358,6 @@ private class RollupServiceTest4 { insert children; // Assert limits - // TODO: Remove debug statements - in-place to get full picture when test fails - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) })); - // + One query on Rollup object // + One query on LookupChild__c for rollup A // + One query on LookupChild__c for rollup B @@ -492,11 +482,6 @@ private class RollupServiceTest4 { insert children; // Assert limits - // TODO: Remove debug statements - in-place to get full picture when test fails - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) })); - // + One query on Rollup object // + One query on LookupChild__c for rollup System.assertEquals(beforeQueries + 2, Limits.getQueries()); @@ -619,11 +604,6 @@ private class RollupServiceTest4 { insert children; // Assert limits - // TODO: Remove debug statements - in-place to get full picture when test fails - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) })); - // + One query on Rollup object // + One query on LookupChild__c for rollup A // + One query on LookupChild__c for rollup B @@ -753,11 +733,6 @@ private class RollupServiceTest4 { insert children; // Assert limits - // TODO: Remove debug statements - in-place to get full picture when test fails - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) })); - // + One query on Rollup object // + One query on LookupChild__c for rollup System.assertEquals(beforeQueries + 2, Limits.getQueries()); @@ -886,11 +861,6 @@ private class RollupServiceTest4 { insert children; // Assert limits - // TODO: Remove debug statements - in-place to get full picture when test fails - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) })); - // + One query on Rollup object // + One query on LookupChild__c for rollup A // + One query on LookupChild__c for rollup B @@ -1020,11 +990,6 @@ private class RollupServiceTest4 { insert children; // Assert limits - // TODO: Remove debug statements - in-place to get full picture when test fails - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) })); - System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) })); - // + One query on Rollup object // + One query on LookupChild__c for rollup A // + One query on LookupChild__c for rollup B diff --git a/rolluptool/src/classes/RollupSummaries.cls b/rolluptool/src/classes/RollupSummaries.cls index 3ebd2882..1c6ca149 100644 --- a/rolluptool/src/classes/RollupSummaries.cls +++ b/rolluptool/src/classes/RollupSummaries.cls @@ -82,6 +82,97 @@ public class RollupSummaries extends fflib_SObjectDomain { super(records); } + + /** + * Before Insert processing + **/ + public override void onBeforeInsert() + { + updateDescribableFieldNames(); + } + + /** + * Before Update processing + **/ + public override void onBeforeUpdate(Map existingRecords) + { + updateDescribableFieldNames(); + } + + /** + * update field names using describe info + **/ + private void updateDescribableFieldNames() + { + Map gd = Schema.getGlobalDescribe(); + Map> gdFields = new Map>(); + for(LookupRollupSummary__c lookupRollupSummary : (List) Records) + { + SObjectType parentObjectType = gd.get(lookupRollupSummary.ParentObject__c); + SObjectType childObjectType = gd.get(lookupRollupSummary.ChildObject__c); + if(parentObjectType!=null && !gdFields.containsKey(parentObjectType)) + gdFields.put(parentObjectType, parentObjectType.getDescribe().fields.getMap()); + if(childObjectType!=null && !gdFields.containsKey(childObjectType)) + gdFields.put(childObjectType, childObjectType.getDescribe().fields.getMap()); + } + + for(LookupRollupSummary__c lookupRollupSummary : (List) Records) + { + // Parent Object + SObjectType parentObjectType = gd.get(lookupRollupSummary.ParentObject__c); + if(parentObjectType!=null) + lookupRollupSummary.ParentObject__c = parentObjectType.getDescribe().getName(); + + // Child Object + SObjectType childObjectType = gd.get(lookupRollupSummary.ChildObject__c); + if(childObjectType!=null) + lookupRollupSummary.ChildObject__c = childObjectType.getDescribe().getName(); + + // Child Object fields + SObjectField relationshipField = null; + SObjectField fieldToAggregate = null; + SObjectField fieldToOrderBy = null; + Map childObjectFields = gdFields.get(childObjectType); + if(childObjectFields!=null) + { + // Relationship field + relationshipField = childObjectFields.get(lookupRollupSummary.RelationshipField__c); + if(relationshipField!=null) + lookupRollupSummary.RelationshipField__c = relationshipField.getDescribe().getName(); + // Field to Aggregate + fieldToAggregate = childObjectFields.get(lookupRollupSummary.FieldToAggregate__c); + if(fieldToAggregate!=null) + lookupRollupSummary.FieldToAggregate__c = fieldToAggregate.getDescribe().getName(); + // Field to Order By + if(lookupRollupSummary.FieldToOrderBy__c!=null) { + fieldToOrderBy = childObjectFields.get(lookupRollupSummary.FieldToOrderBy__c); + if(fieldToOrderBy!=null) + lookupRollupSummary.FieldToOrderBy__c = fieldToOrderBy.getDescribe().getName(); + } + } + // Parent Object fields + SObjectField aggregateResultField = null; + Map parentObjectFields = gdFields.get(parentObjectType); + if(parentObjectFields!=null) + { + // Aggregate Result field + aggregateResultField = parentObjectFields.get(lookupRollupSummary.AggregateResultField__c); + if(aggregateResultField!=null) + lookupRollupSummary.AggregateResultField__c = aggregateResultField.getDescribe().getName(); + } + // Check the list of fields expressed in the relationship critiera fields + if(childObjectFields!=null && lookupRollupSummary.RelationshipCriteriaFields__c!=null) + { + List relationshipCriteriaFields = new List(); + String[] fieldList = lookupRollupSummary.RelationshipCriteriaFields__c.split('\r\n'); + for(String field : fieldList) { + SObjectField relationshipCriteriaField = childObjectFields.get(field); + relationshipCriteriaFields.add(relationshipCriteriaField !=null ? relationshipCriteriaField.getDescribe().getName() : field); + } + lookupRollupSummary.RelationshipCriteriaFields__c = String.join(relationshipCriteriaFields, '\r\n'); + } + } + } /** * Validations for inserts and updates of records