I am trying to setup a schedulable email in an apex class. It queries a custom object (used as an analytic snapshot) then pull the information from the objects to build a formatted email. I am getting stuck in sending the email - when I remove all but a couple of the variables, it sends, otherwise is fails. I guess because it is too many lines? Are there ways to simplify the code?
Code:
public class PentathlonUpdate {
public static void sendMail(string message) {
Map<String, Decimal> Points = new Map<String, Decimal>();
Map<String, Decimal> PointsGoal = new Map<String, Decimal>();
Map<String, Decimal> PointsPercent = new Map<String, Decimal>();
List<Pentathlon_Points__c> PentathlonPoints = [SELECT Team__c, YTD_Points__c, Point_Goal__c, Goal_Percent__c
FROM Pentathlon_Points__c
WHERE CreatedDate = Today];
For(Pentathlon_Points__c PP: PentathlonPoints){
Points.put(PP.Team__c, PP.YTD_Points__c);
PointsGoal.put(PP.Team__c, PP.Point_Goal__c);
PointsPercent.put(PP.Team__c, PP.Goal_Percent__c);
}
Decimal AATTP = Points.ContainsKey('Team 1') ? Points.get('Team 1') : 0;
Decimal AGP = Points.ContainsKey('Team 2') ? Points.get('Team 2') : 0;
Decimal FBP = Points.ContainsKey('Team 3') ? Points.get('Team 3') : 0;
Decimal PHP = Points.ContainsKey('Team 4') ? Points.get('Team 4') : 0;
Decimal STP = Points.ContainsKey('Team 5') ? Points.get('Team 5') : 0;
Decimal WPBCPP = Points.ContainsKey('06 - WPB&CP') ? Points.get('06 - WPB&CP') : 0;
Decimal COP = Points.ContainsKey('Team 7') ? Points.get('Team 7') : 0;
Decimal MSP = Points.ContainsKey('Team 8') ? Points.get('Team 8') : 0;
Decimal MSCBP = Points.ContainsKey('Team 9') ? Points.get('Team 9') : 0;
Decimal PPLP = Points.ContainsKey('Team 10') ? Points.get('Team 10') : 0;
Decimal OFFP = Points.ContainsKey('Team 11') ? Points.get('Team 11') : 0;
Decimal ONEP = Points.ContainsKey('Team 12') ? Points.get('Team 12') : 0;
Decimal ONMP = Points.ContainsKey('Team 13') ? Points.get('Team 13') : 0;
Decimal ONWP = Points.ContainsKey('Team 14') ? Points.get('Team 14') : 0;
Decimal CHEMP = Points.ContainsKey('Team 15') ? Points.get('Team 15') : 0;
Decimal REFP = Points.ContainsKey('Team 16') ? Points.get('Team 16') : 0;
Decimal AUSP = Points.ContainsKey('Team 17') ? Points.get('Team 17') : 0;
Decimal UKP = Points.ContainsKey('Team 18') ? Points.get('Team 18') : 0;
Decimal ECANP = Points.ContainsKey('Team 19') ? Points.get('Team 19') : 0;
Decimal WCANP = Points.ContainsKey('Team 20') ? Points.get('Team 20') : 0;
Decimal UTP = Points.ContainsKey('Team 21') ? Points.get('Team 21') : 0;
Decimal PSP = Points.ContainsKey('Team 22') ? Points.get('Team 22') : 0;
Decimal AATTPG = PointsGoal.ContainsKey('Team 1') ? PointsGoal.get('Team 1') : 0;
Decimal AGPG = PointsGoal.ContainsKey('Team 2') ? PointsGoal.get('Team 2') : 0;
Decimal FBPG = PointsGoal.ContainsKey('Team 3') ? PointsGoal.get('Team 3') : 0;
Decimal PHPG = PointsGoal.ContainsKey('Team 4') ? PointsGoal.get('Team 4') : 0;
Decimal STPG = PointsGoal.ContainsKey('Team 5') ? PointsGoal.get('Team 5') : 0;
Decimal WPBCPPG = PointsGoal.ContainsKey('06 - WPB&CP') ? PointsGoal.get('06 - WPB&CP') : 0;
Decimal COPG = PointsGoal.ContainsKey('Team 7') ? PointsGoal.get('Team 7') : 0;
Decimal MSPG = PointsGoal.ContainsKey('Team 8') ? PointsGoal.get('Team 8') : 0;
Decimal MSCBPG = PointsGoal.ContainsKey('Team 9') ? PointsGoal.get('Team 9') : 0;
Decimal PPLPG = PointsGoal.ContainsKey('Team 10') ? PointsGoal.get('Team 10') : 0;
Decimal OFFPG = PointsGoal.ContainsKey('Team 11') ? PointsGoal.get('Team 11') : 0;
Decimal ONEPG = PointsGoal.ContainsKey('Team 12') ? PointsGoal.get('Team 12') : 0;
Decimal ONMPG = PointsGoal.ContainsKey('Team 13') ? PointsGoal.get('Team 13') : 0;
Decimal ONWPG = PointsGoal.ContainsKey('Team 14') ? PointsGoal.get('Team 14') : 0;
Decimal CHEMPG = PointsGoal.ContainsKey('Team 15') ? PointsGoal.get('Team 15') : 0;
Decimal REFPG = PointsGoal.ContainsKey('Team 16') ? PointsGoal.get('Team 16') : 0;
Decimal AUSPG = PointsGoal.ContainsKey('Team 17') ? PointsGoal.get('Team 17') : 0;
Decimal UKPG = PointsGoal.ContainsKey('Team 18') ? PointsGoal.get('Team 18') : 0;
Decimal ECANPG = PointsGoal.ContainsKey('Team 19') ? PointsGoal.get('Team 19') : 0;
Decimal WCANPG = PointsGoal.ContainsKey('Team 20') ? PointsGoal.get('Team 20') : 0;
Decimal UTPG = PointsGoal.ContainsKey('Team 21') ? PointsGoal.get('Team 21') : 0;
Decimal PSPG = PointsGoal.ContainsKey('Team 22') ? PointsGoal.get('Team 22') : 0;
Decimal AATTPGP = PointsPercent.ContainsKey('Team 1') ? PointsPercent.get('Team 1') : 0;
Decimal AGPGP = PointsPercent.ContainsKey('Team 2') ? PointsPercent.get('Team 2') : 0;
Decimal FBPGP = PointsPercent.ContainsKey('Team 3') ? PointsPercent.get('Team 3') : 0;
Decimal PHPGP = PointsPercent.ContainsKey('Team 4') ? PointsPercent.get('Team 4') : 0;
Decimal STPGP = PointsPercent.ContainsKey('Team 5') ? PointsPercent.get('Team 5') : 0;
Decimal WPBCPPGP = PointsPercent.ContainsKey('06 - WPB&CP') ? PointsPercent.get('06 - WPB&CP') : 0;
Decimal COPGP = PointsPercent.ContainsKey('Team 7') ? PointsPercent.get('Team 7') : 0;
Decimal MSPGP = PointsPercent.ContainsKey('Team 8') ? PointsPercent.get('Team 8') : 0;
Decimal MSCBPGP = PointsPercent.ContainsKey('Team 9') ? PointsPercent.get('Team 9') : 0;
Decimal PPLPGP = PointsPercent.ContainsKey('Team 10') ? PointsPercent.get('Team 10') : 0;
Decimal OFFPGP = PointsPercent.ContainsKey('Team 11') ? PointsPercent.get('Team 11') : 0;
Decimal ONEPGP = PointsPercent.ContainsKey('Team 12') ? PointsPercent.get('Team 12') : 0;
Decimal ONMPGP = PointsPercent.ContainsKey('Team 13') ? PointsPercent.get('Team 13') : 0;
Decimal ONWPGP = PointsPercent.ContainsKey('Team 14') ? PointsPercent.get('Team 14') : 0;
Decimal CHEMPGP = PointsPercent.ContainsKey('Team 15') ? PointsPercent.get('Team 15') : 0;
Decimal REFPGP = PointsPercent.ContainsKey('Team 16') ? PointsPercent.get('Team 16') : 0;
Decimal AUSPGP = PointsPercent.ContainsKey('Team 17') ? PointsPercent.get('Team 17') : 0;
Decimal UKPGP = PointsPercent.ContainsKey('Team 18') ? PointsPercent.get('Team 18') : 0;
Decimal ECANPGP = PointsPercent.ContainsKey('Team 19') ? PointsPercent.get('Team 19') : 0;
Decimal WCANPGP = PointsPercent.ContainsKey('Team 20') ? PointsPercent.get('Team 20') : 0;
Decimal UTPGP = PointsPercent.ContainsKey('Team 21') ? PointsPercent.get('Team 21') : 0;
Decimal PSPGP = PointsPercent.ContainsKey('Team 22') ? PointsPercent.get('Team 22') : 0;
Decimal PointTotal = (AATTP + AGP + FBP + PHP + STP + WPBCPP + COP + MSP + MSCBP + PPLP + OFFP + ONEP + ONMP + ONWP + CHEMP + REFP + AUSP + UKP + ECANP + WCANP + UTP + PSP);
Decimal PointGoalTotal = 410000;
Decimal PointPercentTotal = (PointTotal / PointGoalTotal * 100);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'email@email.com'});
mail.setSubject('Points (TESTING)');
mail.setUseSignature(false);
mail.setHtmlBody(
'<html><font size=2 face=Arial>' +
'<b><u>Points:</u></b><br/>' +
'Team 1: ' + AATTP.format() + ' / ' + AATTPG.format() + ' / ' + AATTPGP.setScale(0) + '%<br/>' +
'Team 2: ' + AGP.format() + ' / ' + AGPG.format() + ' / ' + AGPGP.setScale(0) + '%<br/>' +
'Team 3: ' + FBP.format() + ' / ' + FBPG.format() + ' / ' + FBPGP.setScale(0) + '%<br/>' +
'Team 4: ' + PHP.format() + ' / ' + PHPG.format() + ' / ' + PHPGP.setScale(0) + '%<br/>' +
'Team 5: ' + STP.format() + ' / ' + STPG.format() + ' / ' + STPGP.setScale(0) + '%<br/>' +
'Team 6: ' + WPBCPP.format() + ' / ' + WPBCPPG.format() + ' / ' + WPBCPPGP.setScale(0) + '%<br/>' +
'Team 7: ' + COP.format() + ' / ' + COPG.format() + ' / ' + COPGP.setScale(0) + '%<br/>' +
'Team 8: ' + MSCBP.format() + ' / ' + MSCBPG.format() + ' / ' + MSCBPGP.setScale(0) + '%<br/>' +
'Team 9: ' + MSP.format() + ' / ' + MSPG.format() + ' / ' + MSPGP.setScale(0) + '%<br/>' +
'Team 10: ' + PPLP.format() + ' / ' + PPLPG.format() + ' / ' + PPLPGP.setScale(0) + '%<br/>' +
'Team 11: ' + OFFP.format() + ' / ' + OFFPG.format() + ' / ' + OFFPGP.setScale(0) + '%<br/>' +
'Team 12: ' + ONEP.format() + ' / ' + ONEPG.format() + ' / ' + ONEPGP.setScale(0) + '%<br/>' +
'Team 13: ' + ONMP.format() + ' / ' + ONMPG.format() + ' / ' + ONMPGP.setScale(0) + '%<br/>' +
'Team 14: ' + ONWP.format() + ' / ' + ONWPG.format() + ' / ' + ONWPGP.setScale(0) + '%<br/>' +
'Team 15: ' + CHEMP.format() + ' / ' + CHEMPG.format() + ' / ' + CHEMPGP.setScale(0) + '%<br/>' +
'Team 16: ' + REFP.format() + ' / ' + REFPG.format() + ' / ' + REFPGP.setScale(0) + '%<br/>' +
'Team 17: ' + AUSP.format() + ' / ' + AUSPG.format() + ' / ' + AUSPGP.setScale(0) + '%<br/>' +
'Team 18: ' + UKP.format() + ' / ' + UKPG.format() + ' / ' + UKPGP.setScale(0) + '%<br/>' +
'Team 19: ' + ECANP.format() + ' / ' + ECANPG.format() + ' / ' + ECANPGP.setScale(0) + '%<br/>' +
'Team 20: ' + WCANP.format() + ' / ' + WCANPG.format() + ' / ' + WCANPGP.setScale(0) + '%<br/>' +
'Team 21: ' + UTP.format() + ' / ' + UTPG.format() + ' / ' + UTPGP.setScale(0) + '%<br/>' +
'Team 22: ' + PSP.format() + ' / ' + PSPG.format() + ' / ' + PSPGP.setScale(0) + '%<br/>' +
'<b>TOTAL: ' + PointTotal.format() + ' / ' + PointGoalTotal.format() + ' / ' + PointPercentTotal.setScale(0) + '%</b><br/>' +
'</font></html>');
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
public static testMethod void testSendMail() {
sendMail('This is my email message');
}
}