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') ;


 myDate------------->2016-05-02 16:44:41
 convertedDate------------->2016-05-02 11:14: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

1 Answer 1


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.


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

  • Thank you for your time and suggestion .This just gives whether the created date is with in the business hour or not and return true if so . But my requirement is that ,i need to get the exact time difference between the current time stamp and the end time stamp for a particular day of the business hours object. to be very simple i just need to convert the existing Time record(that is stored in string) to a variable with the data type as 'Time'. I need to do this in order to compare my var with the standard 'Business Hours' object fields(Ex: MondayStartTime , etc). String ---> Time May 5, 2016 at 12:55

You must log in to answer this question.

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