[SalesForce] Need to find whether the time stamp is between the business hours

I am trying to implement SLA for Lead management.

So handling all the SLA functionalities on a trigger and Apex class.

I need help in getting the current time and comparing it to the defined business hours.(I am using the standard business hours object and values are already defined for each day).

The business hours object has the values stored in as data-type time.
I have already pulled out the created date from which time values have been extracted, but i am able to store it only in a string field. Any one knows to get the time value and store it in the time field.

   String reqioredtime = convertedDate.format('HH:mm:ss') ;

Output

 myDate------------->2016-05-02 16:44:41
 convertedDate------------->2016-05-02 11:14:41
 reqioredtime------------->16:44:41

I need the same above value in a Time data-type field.
I need this time value in order to compare it to the business hours.

If any one has come across such a situation kindly hep! thank you

My code (for now )

public class lead_SLA_cls {

    List < BusinessHours > hrs = new List < BusinessHours > ();

    public time monStart, monEnd;
    public time getcurrenttime;
    public datetime leaCreatedDate;

    public void Business_hrs() {
        User currentUser = [Select TimeZoneSidKey, country from User where id =: USerInfo.getUserId()];
        System.debug('currentUser------------->' + currentUser);

        hrs = [SELECT Name, MondayStartTime, MondayEndTime, TuesdayStartTime, TuesdayEndTime, WednesdayStartTime, WednesdayEndTime, ThursdayStartTime, ThursdayEndTime, FridayStartTime, FridayEndTime, SundayStartTime, SundayEndTime, SaturdayStartTime, SaturdayEndTime FROM BusinessHours where Name = 'Indian Business Hours'];

        System.debug('hrs------------->' + hrs);

        for (BusinessHours bushr: hrs) {

            monStart = bushr.MondayStartTime;
            monEnd = bushr.MondayEndTime;

        }
        System.debug('monStart------------->' + monStart);
        System.debug('monEnd------------->' + monEnd);

    }

    public void calculateLeadSLAinsert(List < Lead > lea) {

        leaCreatedDate = DateTime.now();
        System.debug('leaCreatedDate------------->' + leaCreatedDate);

        String myDate = leaCreatedDate.format('yyyy-MM-dd HH:mm:ss', 'IST');
        System.debug('myDate------------->' + myDate);

        Datetime convertedDate = datetime.valueOf(myDate);

        System.debug('convertedDate------------->' + convertedDate);


        String timeSec = convertedDate.format('HH:mm:ss');

        getcurrenttime = convertedDate.format('HH:mm:ss');

        System.debug('reqioredtime------------->' + timeSec);
        System.debug('getcurrenttime------------->' + getcurrenttime);

        String dayOfWeek = convertedDate.format('EEEE');
        System.debug('Day ----------------->' + dayOfWeek);

        if (dayOfWeek != 'Sunday') {
            if (dayOfWeek == 'Monday') {

            }

        }
    }
}

Thanks and Regards
Venkatesh

Best Answer

Maybe I'm totally missing the point, but why not just use this built-in function:

public static Boolean BusinessHours.isWithin(String businessHoursId, Datetime targetDate) 

Returns true if the specified target date occurs within business hours. Holidays are included in the calculation.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm

I can't really make any sense of your code, but this seems to satisfy your requirement at the top.

Related Topic