Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
#1
15 - Done

#1
08 - Done
#1
07 - Done
#1
03 - Done (Upgraded to latest API)
  • Loading branch information
afawcettffdc committed Feb 7, 2015
1 parent fb39469 commit c6196e3
Show file tree
Hide file tree
Showing 47 changed files with 166 additions and 106 deletions.
2 changes: 1 addition & 1 deletion rolluptool/src/classes/ApexClassesSelector.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/ApexTriggersSelector.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/AsyncApexJobsSelector.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>24.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
24 changes: 22 additions & 2 deletions rolluptool/src/classes/LREngine.cls
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class LREngine {
for(sObject ct : Database.query(query))
currencyConversionMap.put((String)ct.get('IsoCode'), (Decimal)ct.get('ConversionRate'));
}
return val * currencyConversionMap.get(iso);
return val!=null ? val * currencyConversionMap.get(iso) : 0;
}

/**
Expand All @@ -82,7 +82,7 @@ public class LREngine {
}
return rollUp(ctx, masterRecordIds);
}

/**
Key driver method that rolls up lookup fields based on the context. This is meant to be called from non trigger contexts like
scheduled/batch apex, where we want to rollup on some master record ids.
Expand All @@ -93,6 +93,20 @@ public class LREngine {
because we want client or calling code to have this freedom to do some post processing and update when required.
*/
public static Sobject[] rollUp(Context ctx, Set<Id> masterIds) {
return rollUp(ctx, masterIds, false);
}

/**
Key driver method that rolls up lookup fields based on the context. This is meant to be called from non trigger contexts like
scheduled/batch apex, where we want to rollup on some master record ids.
@param Context the complete context required to rollup
@param masterIds Master record IDs whose child records should be rolled up.
@param validateOnly Checks the SOQL query is valid, throws an exception if not, returns null if valid
@returns Array of in memory master objects. These objects are not updated back to the database
because we want client or calling code to have this freedom to do some post processing and update when required.
*/
public static Sobject[] rollUp(Context ctx, Set<Id> masterIds, Boolean validateOnly) {
// Clone this since we are about to modify it later
masterIds = masterIds.clone();
// K: Id of master record
Expand Down Expand Up @@ -141,6 +155,12 @@ public class LREngine {
String soql = String.format(SOQL_TEMPLATE, new String[]{soqlProjection, detailTblName, whereClause, grpByFld});
System.debug('SOQL is ' + soql);

// validate only?
if(validateOnly) {
Database.getQueryLocator(soql);
return null;
}

// aggregated results
List<AggregateResult> results = Database.query(soql);

Expand Down
2 changes: 1 addition & 1 deletion rolluptool/src/classes/LREngine.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/MetadataService.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/MetadataServiceTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupCalculateJob.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupController.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupControllerTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupJob.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupJobTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupService.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupServiceException.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
29 changes: 3 additions & 26 deletions rolluptool/src/classes/RollupServiceTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -680,15 +680,13 @@ private with sharing class RollupServiceTest
// Sample various limits prior to an update
Integer beforeQueries = Limits.getQueries();
Integer beforeRows = Limits.getQueryRows();
Integer beforeDescribes = Limits.getFieldsDescribes();

// Update opportunities (no changes to the field being aggregted, thus no rollup processng)
update opps;

// Assert no further limits have been used since the field to aggregate on the detail has not changed
System.assertEquals(beforeQueries + 1, Limits.getQueries()); // Only tolerate a query for the Lookup definition
System.assertEquals(beforeRows + 1, Limits.getQueryRows()); // Only tolerate a row for the Lookup definition
System.assertEquals(beforeDescribes, Limits.getFieldsDescribes()); // Zero tolerance on field describes
}

private testmethod static void testLimitsConsumedWithConditions()
Expand Down Expand Up @@ -762,24 +760,18 @@ private with sharing class RollupServiceTest

// One query on ApexTrigger (in the TestContext.isSupported method)
// One query on ApexTrigger (validation when inserting rollups)
// One query on Database.newQueryLocator (validation when insert rollup a)
// One query on Rollup object
// One query on Opportunity for rollup a
// One query on Opportunity for rollup b
System.assertEquals(5, Limits.getQueries());
System.assertEquals(6, Limits.getQueries());

// One row for ApexTrigger (in the TestContext.isSupported method)
// One row for ApexTrigger
// Two rows for Rollup object
// Two rows for Opportunity for rollup a
// Four rows for Opportunity for rollup b
System.assertEquals(10, Limits.getQueryRows());

// One for the statics at the top of the test
// One for the parent object validation on insert of lookup
// One for the child object validaiton on insert of lookup
// One for the lookup of the field type for the parent object during trigger
// One for the lookup of the field type for the child object during trigger
System.assertEquals(5, Limits.getFieldsDescribes());

// Assert rollup
Id accountId = account.Id;
Expand All @@ -795,16 +787,12 @@ private with sharing class RollupServiceTest
// + One query for the Account query above
// + One query on Rollup object
// + One query on Opportunity for rollup a
System.assertEquals(8, Limits.getQueries());
System.assertEquals(9, Limits.getQueries());

// + One query for the Account query above
// + Two rows for Rollup object
// + Two rows for Opportunity for rollup a
System.assertEquals(15, Limits.getQueryRows());

// + One for the lookup of the field type for the parent object during trigger
// + One for the lookup of the field type for the child object during trigger
System.assertEquals(7, Limits.getFieldsDescribes());

// Assert rollup
accountResult = Database.query('select AnnualRevenue, NumberOfLocations__c from Account where Id = :accountId');
Expand Down Expand Up @@ -892,13 +880,6 @@ private with sharing class RollupServiceTest
// Two rows for Rollup object
// Four rows for Opportunity for rollup a and b
System.assertEquals(8, Limits.getQueryRows());

// One for the statics at the top of the test
// One for the parent object validation on insert of lookup
// One for the child object validaiton on insert of lookup
// One for the lookup of the field type for the parent object during trigger
// One for the lookup of the field type for the child object during trigger
System.assertEquals(5, Limits.getFieldsDescribes());

// Assert rollup
Id accountId = account.Id;
Expand All @@ -920,10 +901,6 @@ private with sharing class RollupServiceTest
// + Two rows for Rollup object
// + Four rows for Opportunity for rollup a and
System.assertEquals(15, Limits.getQueryRows());

// + One for the lookup of the field type for the parent object during trigger
// + One for the lookup of the field type for the child object during trigger
System.assertEquals(7, Limits.getFieldsDescribes());

// Assert rollup
accountResult = Database.query('select AnnualRevenue, NumberOfLocations__c from Account where Id = :accountId');
Expand Down
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupServiceTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupServiceTest2.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupServiceTest3.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
42 changes: 30 additions & 12 deletions rolluptool/src/classes/RollupSummaries.cls
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,8 @@ public with sharing class RollupSummaries extends SObjectDomain
**/
public override void onValidate()
{
// Query for any related Apex triggers
// Cache Apex Describes and calculate child object tigger names
Set<String> rollupTriggerNames = new Set<String>();
for(LookupRollupSummary__c lookupRollupSummary : (List<LookupRollupSummary__c>) Records)
rollupTriggerNames.add(makeTriggerName(lookupRollupSummary));
Map<String, ApexTrigger> apexTriggers = new ApexTriggersSelector().selectByName(rollupTriggerNames);

// Cache Apex Describes
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Map<SObjectType, Map<String, Schema.SObjectField>> gdFields = new Map<SObjectType, Map<String, Schema.SObjectField>>();
for(LookupRollupSummary__c lookupRollupSummary : (List<LookupRollupSummary__c>) Records)
Expand All @@ -94,7 +89,13 @@ public with sharing class RollupSummaries extends SObjectDomain
gdFields.put(parentObjectType, parentObjectType.getDescribe().fields.getMap());
if(childObjectType!=null && !gdFields.containsKey(childObjectType))
gdFields.put(childObjectType, childObjectType.getDescribe().fields.getMap());
// Calculate trigger name child object reqquires in order to check existance
if(childObjectType!=null)
rollupTriggerNames.add(makeTriggerName(lookupRollupSummary));
}

// Query for any related Apex triggers
Map<String, ApexTrigger> apexTriggers = new ApexTriggersSelector().selectByName(rollupTriggerNames);

for(LookupRollupSummary__c lookupRollupSummary : (List<LookupRollupSummary__c>) Records)
{
Expand Down Expand Up @@ -134,12 +135,15 @@ public with sharing class RollupSummaries extends SObjectDomain
lookupRollupSummary.AggregateResultField__c.addError(error('Field does not exist.', lookupRollupSummary, LookupRollupSummary__c.AggregateResultField__c));
}
// Cannot activate Realtime or Scheduled rollup without the required trigger deployed
String triggerName = makeTriggerName(lookupRollupSummary);
if(lookupRollupSummary.Active__c &&
(lookupRollupSummary.CalculationMode__c == CalculationMode.Realtime.name() ||
lookupRollupSummary.CalculationMode__c == CalculationMode.Scheduled.name()) &&
!apexTriggers.containsKey(triggerName))
lookupRollupSummary.Active__c.addError(error('Apex Trigger ' + triggerName + ' has not been deployed. Click Manage Child Trigger and try again.', lookupRollupSummary, LookupRollupSummary__c.Active__c));
if(childObjectType!=null)
{
String triggerName = makeTriggerName(lookupRollupSummary);
if(lookupRollupSummary.Active__c &&
(lookupRollupSummary.CalculationMode__c == CalculationMode.Realtime.name() ||
lookupRollupSummary.CalculationMode__c == CalculationMode.Scheduled.name()) &&
!apexTriggers.containsKey(triggerName))
lookupRollupSummary.Active__c.addError(error('Apex Trigger ' + triggerName + ' has not been deployed. Click Manage Child Trigger and try again.', lookupRollupSummary, LookupRollupSummary__c.Active__c));
}
// Check the list of fields expressed in the relationship critiera fields
if(childObjectFields!=null && lookupRollupSummary.RelationshipCriteriaFields__c!=null)
{
Expand Down Expand Up @@ -173,6 +177,18 @@ public with sharing class RollupSummaries extends SObjectDomain
aggregateResultField.getDescribe(),
fieldToAggregate.getDescribe(),
OPERATION_PICKLIST_TO_ENUMS.get(lookupRollupSummary.AggregateOperation__c)));
// Validate the SOQL
if(lookupRollupSummary.RelationShipCriteria__c!=null &&
lookupRollupSummary.RelationShipCriteria__c.length()>0)
{
try {
// Validate only mode ensures no query is actually made should it be valid
LREngine.rollUp(lreContext, new Set<Id>(), true);
} catch (QueryException e) {
lookupRollupSummary.RelationShipCriteria__c.addError(
error(String.format(MSG_INVALID_CRITERIA, new String[] { lookupRollupSummary.RelationShipCriteria__c, e.getMessage() }), lookupRollupSummary, LookupRollupSummary__c.RelationShipCriteria__c));
}
}
}
}
catch (LREngine.BadRollUpSummaryStateException e)
Expand All @@ -182,6 +198,8 @@ public with sharing class RollupSummaries extends SObjectDomain
}
}
}

private static final String MSG_INVALID_CRITERIA = 'Relationship Criteria \'\'{0}\'\' is not valid, see SOQL documentation http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_conditionexpression.htm, error is \'\'{1}\'\'';

public class Constructor implements SObjectDomain.IConstructable
{
Expand Down
2 changes: 1 addition & 1 deletion rolluptool/src/classes/RollupSummaries.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>32.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading

0 comments on commit c6196e3

Please sign in to comment.