My requirement is to assign records if the lastmodifiedDate of the record falls within the employee availability. For example let's say on Sunday employee available between 8.00 AM to 5.00 PM and if the record modified falls within the time then system can assign the record to the user.

To achieve this I am taking day(as to compare against each day checkin/out hour) & time portion from the LastModifiedDate field and trying to compare the time against that by the user definition.

This what I have done so far but I am doubting myself whether I am in right path.

for(Distribution_Team_Member__c dtmObj: dtmList){
    //LastModified time value of the record
    Time lastMoTime = recInfos.get(0).lmDatetime.time();
    //Start hour from the employee definition record
    Time dtmStartHour = (Time)dtmObj.get(dtmDayStartHour);
    //End hour from the employee definition record
    Time dtmEndHour   = (Time)dtmObj.get(dtmDayEndHour);
    Integer startTimeMinElapsed = GetHourElapsed(lastMoTime,dtmStartHour);
    Integer endTimeMinElapsed   = GetHourElapsed(lastMoTime,dtmEndHour);

public static Time GetElapsedTime(Time startTime, Time endTime)
    if(startTime == null || endTime == null)
        return Time.newInstance(0, 0, 0, 0);

    Integer elapsedHours = endTime.hour() - startTime.hour();
    Integer elapsedMinutes = endTime.minute() - startTime.minute();
    Integer elapsedSeconds = endTime.second() - startTime.second();
    Integer elapsedMiliseconds = endTime.millisecond() - startTime.millisecond();

    return Time.newInstance(elapsedHours, elapsedMinutes, elapsedSeconds, elapsedMiliseconds);

public static Integer GetHourElapsed(Time startHour, Time endHour)
    return GetElapsedTime(startHour,endHour).hour();

enter image description here

enter image description here

  • What are you specifically looking for? Whether this is the best approach or is there an error with what you've done you're receiving? – Kris Goncalves Dec 8 '19 at 16:45
  • I am trying to find the best approach to identify whether LastModifiedDate Time falls with in Check in and Check out time of an employee. Let's say record LastModifiedDate 11.50 PM and employee availability is 8.00 AM to 5.00 PM then its eligible for record assignment. – user28452 Dec 9 '19 at 4:04
  • You're making this too hard -- for each dateTime, convert to an epoch using getTime() and then do a simple comparison of the resulting longs – cropredy Dec 9 '19 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.