1

I am trying to increment the value of fields over time. Basically, bi-weekly it needs to increment the value multiplied by another field value. Is there "Case" or "Switch" in Salesforce?

This is the code I have. Too many lines. Any suggestions are welcomed!

trigger ThriveHRPTO_ADD_HOURS on Thrive_HR_PTO_Record__c (before insert, before update) {
  for (Thrive_HR_PTO_Record__c pto : Trigger.new){ 

    // January First Week
    if(system.today() >= pto.Calendar_Year_Start__c && system.today() <= pto.Calendar_Year_Start__c.addDays(13)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c;
    }
    // January Second Week
    else if(system.today() >= pto.Calendar_Year_Start__c.addDays(14) && system.today() <= pto.Calendar_Year_Start__c.addDays(30)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 2;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 2;
    }
    // February First Week
    else if(system.today() >= pto.Calendar_Year_Start__c.addDays(31) && system.today() <= pto.Calendar_Year_Start__c.addDays(43)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 3;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 3;
    }
    // February Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(44) && system.today() <= pto.Calendar_Year_Start__c.addDays(57)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 4;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 4;
    }
    // March First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(58) && system.today() <= pto.Calendar_Year_Start__c.addDays(70)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 5;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 5;
    }
    // March Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(71) && system.today() <= pto.Calendar_Year_Start__c.addDays(87)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 6;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 6;
    }
    // April First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(88) && system.today() <= pto.Calendar_Year_Start__c.addDays(100)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 7;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 7;
    }
    // April Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(101) && system.today() <= pto.Calendar_Year_Start__c.addDays(116)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 8;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 8;
    }
    // May First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(117) && system.today() <= pto.Calendar_Year_Start__c.addDays(129)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 9;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 9;
    }
    // May Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(130) && system.today() <= pto.Calendar_Year_Start__c.addDays(146)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 10;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 10;
    }
    // June First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(147) && system.today() <= pto.Calendar_Year_Start__c.addDays(159)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 11;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 11;
    }
    // June Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(160) && system.today() <= pto.Calendar_Year_Start__c.addDays(175)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 12;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 12;
    }
    // July First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(176) && system.today() <= pto.Calendar_Year_Start__c.addDays(188)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 13;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 13;
    }
    // July Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(189) && system.today() <= pto.Calendar_Year_Start__c.addDays(205)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 14;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 14;
    }
    // August First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(206) && system.today() <= pto.Calendar_Year_Start__c.addDays(218)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 15;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 15;
    }
    // August Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(219) && system.today() <= pto.Calendar_Year_Start__c.addDays(235)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 16;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 16;
    }
    // September First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(236) && system.today() <= pto.Calendar_Year_Start__c.addDays(248)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 17;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 17;
    }
    // September Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(249) && system.today() <= pto.Calendar_Year_Start__c.addDays(264)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 18;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 18;
    }
    // October First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(265) && system.today() <= pto.Calendar_Year_Start__c.addDays(277)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 19;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 19;
    }
    // October Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(278) && system.today() <= pto.Calendar_Year_Start__c.addDays(294)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 20;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 20;
    }
    // November First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(295) && system.today() <= pto.Calendar_Year_Start__c.addDays(307)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 21;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 21;
    }
    // November Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(308) && system.today() <= pto.Calendar_Year_Start__c.addDays(323)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 22;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 22;
    }
    // Decemeber First Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(324) && system.today() <= pto.Calendar_Year_Start__c.addDays(336)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 23;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 23;
    }
    // Decemeber Second Week
    else if (system.today() >= pto.Calendar_Year_Start__c.addDays(337) && system.today() <= pto.Calendar_Year_Start__c.addDays(353)){
        pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * 24;
        pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * 24;
    }
  }
}

2 Answers 2

0

Apex does not currently have a case statement. Here's a reference on that topic.

One comment notes that "case" is already keyword in Apex, so if we ever get a case statement, it will probably be called something else. ;-)

0

Your can separate out the constants that apply to each case from how the constants are applied to reduce the amount of code.

The trigger calls a class:

trigger ThriveHRPTO_ADD_HOURS on Thrive_HR_PTO_Record__c (before insert, before update) {
    MyTriggerHelperClass.calculateAccumulatedHours(trigger.new);
}

that does the work:

public class MyTriggerHelperClass {
    private class Period {
        Integer start;
        Integer end;
        Integer multiplier;
        Period(Integer start, Integer end, Integer multiplier) {
            this.start = start;
            this.end = end;
            this.multiplier = multiplier;
        }
    }
    private static final Period[] PERIODS = new Period[] {
        new Period(0, 13, 1),
        new Period(14, 30, 2),
        ...
    };
    public static void calculateAccumulatedHours(Thrive_HR_PTO_Record__c[] ptos) {
        Date today = Date.today();
        for (Thrive_HR_PTO_Record__c pro : ptos) {
            for (Period p : PERIODS) {
               if (today >= pto.Calendar_Year_Start__c.addSays(p.start)
                       && today <= pto.Calendar_Year_Start__c.addSays(p.end)) {
                   pto.Accumulated_Hours__c = pto.Accumulation_Increment_Hours__c * p.multiplier;
                   pto.Accumulated_Sick_Hours__c = pto.Accumulation_Increment_Sick_Hours__c * p.multiplier;
                   break;
               }
            }
        }
    }
}
1
  • wow! thank you so much Keith! I am subscribing to your site too. So much resources! Commented Sep 10, 2014 at 14:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .