0

I have a simple PTO(Paid Time Off) object, in which I want certain fields to automatically add increments over time evaluated by the beginning of the Calendar Year.

So for example, if my beginning calendar year is 1/1/2014 in it will start with 0 PTO accumulated hours. Bi weekly, I want the accumulated hours to go up in increments of 5 hours. So when it is 2/1/2014, it should have 10 hours accumulated.

I was able to do it, but it only occurs when record is created, or updated. I want to create a record, and see it automatically update without updating the record. I hope that is making sense.

Here is my trigger

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;
    }
    // December 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;
    }
    // December 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;
    }

}
}

And here are my records. As you can see, it only updates when I went into the record and save the record.

enter image description here

enter image description here

1
  • You can use schedule apex ..and write your own logic there .!!
    – Vineeth
    Oct 8, 2014 at 21:14

3 Answers 3

1

You're going to need to implement this with an Apex Scheduler that runs periodically. Every time this class runs, you can retrieve your active PTO records and calculate each PTO's accumulation of hours relative to their start date.

Apex Scheduler Reference: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

0
1

You can use schedule apex ..and write your own logic there .!!and you can schedule your logic like you said above so that it updates all the fields as per your desired hours in PTO's accumulation on Hour bases . as mentioned by @jmrjulian you can use the reference link and write your logic .

hope this helps you to solve your problem

Cheers

Vineeth

0

In Salesforce, a field is either set by a DML operation or computed when queried/viewed.

Triggers will only execute when a DML operation occurs. Such a DML operation occurs in many ways, notably:

  1. User save operation on a page
  2. Batch job (or scheduled batch job) that does DML against records of interest to the batch
  3. Field update via a workflow (timed or otherwise)
  4. some client invokes the SFDC API (SOAP or REST)
  5. Dataloader or other batch updater

Formula fields compute their value at runtime based on other values in the object or accessible via lookup objects. Formula fields can not invoke web services or triggers to aid in their computation

To resolve your question, without doing database updates will require a formula field that is aware of the current year and current date

You must log in to answer this question.

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