Skip to content

Commit

Permalink
Minor usability improvement when tracking scheduled calc jobs
Browse files Browse the repository at this point in the history
#4
15
#3
53
  • Loading branch information
Andrew Fawcett authored and Andrew Fawcett committed Feb 19, 2017
1 parent 08e99fa commit 682f58f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
17 changes: 16 additions & 1 deletion rolluptool/src/classes/RollupScheduledCalculateController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,30 @@ public with sharing class RollupScheduledCalculateController
public List<String> day { get; set; }
public List<String> month { get; set; }
public List<String> weekday { get; set; }
public String uniqueNameForJob { get; set; }

public RollupScheduledCalculateController (ApexPages.StandardController standardController)
{
// Since thie controller is shared between custom object and custom mestadata, determine the applicable unique field
String uniqueNameField =
((Id)standardController.getId()).getSObjectType() == Schema.LookupRollupSummary2__mdt.SObjectType
? 'DeveloperName' : 'UniqueName__c';

// Ensure the unique name field is included in the standard controller read fields
this.standardController = standardController;
if(!Test.isRunningTest()) { // See https://developer.salesforce.com/forums/?id=906F00000008ytQIAQ
this.standardController.addFields(new List<String> { uniqueNameField });
}

// Calculate unique name
String rollupUniqueName = (String) standardController.getRecord().get(uniqueNameField);
uniqueNameForJob = 'rollup_' + (rollupUniqueName==null || rollupUniqueName.equals('') ? standardController.getId() : rollupUniqueName + ' (' + standardController.getId() + ')');

// Page load messages
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Clicking the Schedule Calculate Job will schedule a recurring background rebuild for this rollup only.'));
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'The default shown below will run the Calculate job at 2am every day.'));
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Once the job completes any failed rollups will be shown in the Rollup Summary Logs tab.'));
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'The Calculate job can be managed at Setup -> Jobs -> Scheduled Jobs; its name will be ' + uniqueNameForJob));

// Minutes
minutes = new List<SelectOption>();
Expand Down Expand Up @@ -75,7 +90,7 @@ public with sharing class RollupScheduledCalculateController
' ');

// Kickoff the calculate job for this lookup
System.schedule('rollup_'+standardController.getId(), cronString, new RollupCalculateJobSchedulable(standardController.getId(), masterObjectWhere));
System.schedule(uniqueNameForJob, cronString, new RollupCalculateJobSchedulable(standardController.getId(), masterObjectWhere));
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Calculate job scheduled for this lookup.'));
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ private class RollupScheduledCalculateControllerTest {
System.assertEquals('rollup_'+standardController.getId(), cronTrigger.CronJobDetail.Name);
}

@IsTest
private static void testCronHelperDefaultWithUniqueName() {

// Between Task and Account
LookupRollupSummary__c rollupSummaryAccount = new LookupRollupSummary__c();
rollupSummaryAccount.Name = 'Test Rollup';
rollupSummaryAccount.UniqueName__c = 'TestRollup';
rollupSummaryAccount.ParentObject__c = 'Account';
rollupSummaryAccount.ChildObject__c = 'Task';
rollupSummaryAccount.RelationShipField__c = 'WhatId';
rollupSummaryAccount.FieldToAggregate__c = 'Id';
rollupSummaryAccount.AggregateOperation__c = RollupSummaries.AggregateOperation.Count.name();
rollupSummaryAccount.AggregateResultField__c = 'AnnualRevenue';
rollupSummaryAccount.Active__c = true;
rollupSummaryAccount.CalculationMode__c = 'Scheduled';
insert rollupSummaryAccount;

// Test default behaviour
Test.startTest();
ApexPages.StandardController standardController =
new ApexPages.StandardController(rollupSummaryAccount);
RollupScheduledCalculateController controller =
new RollupScheduledCalculateController(standardController);
controller.scheduleCalculateJob();
Test.stopTest();

// Assert correct Cron job has been inserted
CronTrigger cronTrigger = [select Id, CronJobDetail.Name, CronExpression from CronTrigger Limit 1][0];
System.assertEquals('0 0 2 * * ?', cronTrigger.CronExpression);
System.assertEquals('rollup_TestRollup (' + standardController.getId() + ')', cronTrigger.CronJobDetail.Name);
}

@IsTest
private static void testCronHelperError() {

Expand Down Expand Up @@ -87,7 +119,7 @@ private class RollupScheduledCalculateControllerTest {
// Assert nothing has been scheduled and we got an error
System.assertEquals(0, [select Id, CronJobDetail.Name, CronExpression from CronTrigger].size());
System.assert(ApexPages.hasMessages(ApexPages.Severity.Error));
System.assertEquals('\'?\' can only be specified for Day-of-Month -OR- Day-of-Week.', ApexPages.getMessages()[3].getDetail());
System.assertEquals('\'?\' can only be specified for Day-of-Month -OR- Day-of-Week.', ApexPages.getMessages()[4].getDetail());
}


Expand Down

0 comments on commit 682f58f

Please sign in to comment.