function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
salesforce_hoonigansalesforce_hoonigan 

Apex Help: Adding Number field and Date/Time respecting Business Hours/Day

Hi All,

Please bear with me since I am a complete newbie with Apex. I currently have a code and I can't get it to work. Need help.

Requirement: Add Number field and Date/Field respecting the Business Hours/Day and decimals in number field

Detail: 
Business Hours: 7:00 AM - 5:00 PM   ---- Total of 10 hrs.
Actual_Time__c (Date/Time) 
Hours__c (Number field with two decimal places)
Target_Date_Time__c (Date/Time) 

Ex. 1
Actual_Time__c + Hours__c = Target_Date_Time__c 
01/10/2015 7:00 AM + 2.00 = 01/10/2015 9:00 AM

Ex. 2 (respecting Business Hours/Day)
Actual_Time__c + Hours__c = Target_Date_Time__c 
01/10/2015 4:00 PM + 2.00 = 02/10/2015 8:00 AM

Ex. 2 (respecting Business Hours/Day with decimal)
Actual_Time__c + Hours__c = Target_Date_Time__c 
01/10/2015 4:30 PM + 2.00 = 02/10/2015 8:30 AM

CODE:
trigger TargetCompletionTime on Predecessor_Task__c (before insert, before update) {
    
    for(Predecessor_Task__c pt : trigger.new)
        {
            BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
            DateTime d1 = pt.Actual_Time__c ;
            
            if((pt.Actual_Time__c !=null) && (pt.Hours__c !=null)) {
            
            pt.Target_Date_Time__c  = d1.addHours(integer.valueOf(pt.Hours__c));
   }
}
}





 
Best Answer chosen by salesforce_hoonigan
Alexander TsitsuraAlexander Tsitsura
Maybe problem with time zones, becouse i try use code and i have corect result
User-added image
Please virufy that you use correct trigger code?
trigger TargetCompletionTime on Predecessor_Task__c (before insert, before update) {
    BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
    for(Predecessor_Task__c pt : trigger.new)
    {
        if((pt.Actual_Time__c !=null) && (pt.Hours__c !=null)) 
        {
            Datetime nextStart = BusinessHours.add(stdBusinessHours.Id, pt.Actual_Time__c, integer.valueOf(pt.Hours__c) * 60 * 60 * 1000);
            pt.Target_Date_Time__c  = nextStart;
        }
        
    }
}

 

All Answers

salesforce_hoonigansalesforce_hoonigan
Hi Alexander,

It still doesn't work. It doesn't follow Business Hours.

Current Behavior:
01/10/2015 4:00 PM + 2.00 = 01/10/2015 6:00 PM

Expected Result
01/10/2015 4:00 PM + 2.00 = 02/10/2015 8:00 AM

Thank you.
Alexander TsitsuraAlexander Tsitsura
Hi salesforce_hoonigan, sorry i forgot aboud BusinessHours

You need avoid soql in loop, i recomended you to read apex best practise https://developer.salesforce.com/page/Apex_Code_Best_Practices

Try this code
trigger TargetCompletionTime on Predecessor_Task__c (before insert, before update) {
    BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
    for(Predecessor_Task__c pt : trigger.new)
    {
        if((pt.Actual_Time__c !=null) && (pt.Hours__c !=null)) 
        {
            DateTime targetTime = pt.Actual_Time__c.addHours(integer.valueOf(pt.Hours__c));
            Datetime nextStart = BusinessHours.nextStartDate(stdBusinessHours.id, targetTime);
            pt.Target_Date_Time__c  = nextStart;
        }
        
    }
}


As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex

 
salesforce_hoonigansalesforce_hoonigan
Hi @Alex,

Still doesn't work.
Alexander TsitsuraAlexander Tsitsura
What not work? error message or target date not expected?
Alexander TsitsuraAlexander Tsitsura
Maybe problem with TimeZine.
Go to Setup -> Company Profile -> Business Hours -> find Default Business Hours and click on Default name -> Please view Time Zone field,

and after that review your Time Zone: Your Name -> My Settings -> Personal -> Language & Time Zone -> and change time zone to business hour time zone, and retest trigger.

Thanks
salesforce_hoonigansalesforce_hoonigan
Hi Alex,

My Timezone is set correctly, regardless of any value entered in Hours__c , it results to 01/10/2015 7:00 AM
Alexander TsitsuraAlexander Tsitsura
Maybe result is 02/10/2015 7:00 AM insted of 01/10/2015 7:00 AM?
 
Alexander TsitsuraAlexander Tsitsura
Can you try this code?
 
trigger TargetCompletionTime on Predecessor_Task__c (before insert, before update) {
    BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
    for(Predecessor_Task__c pt : trigger.new)
    {
        if((pt.Actual_Time__c !=null) && (pt.Hours__c !=null)) 
        {
            Datetime nextStart = BusinessHours.add(stdBusinessHours.Id, pt.Actual_Time__c, integer.valueOf(pt.Hours__c) * 60 * 60 * 1000);
            pt.Target_Date_Time__c  = nextStart;
        }
        
    }
}
As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex

 
salesforce_hoonigansalesforce_hoonigan
Hi Alex,

Thank you for your patience. Doesn't seem to be accurate. When I enter 30/09/2015 7:00 AM (Actual_Time__c) and 2.00 (Hours__c), result is 01/10/2015 9:00 AM. It should be 30/09/2015 9:00 AM
Alexander TsitsuraAlexander Tsitsura
Maybe problem with time zones, becouse i try use code and i have corect result
User-added image
Please virufy that you use correct trigger code?
trigger TargetCompletionTime on Predecessor_Task__c (before insert, before update) {
    BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
    for(Predecessor_Task__c pt : trigger.new)
    {
        if((pt.Actual_Time__c !=null) && (pt.Hours__c !=null)) 
        {
            Datetime nextStart = BusinessHours.add(stdBusinessHours.Id, pt.Actual_Time__c, integer.valueOf(pt.Hours__c) * 60 * 60 * 1000);
            pt.Target_Date_Time__c  = nextStart;
        }
        
    }
}

 
This was selected as the best answer
Alexander TsitsuraAlexander Tsitsura
Plz let me know if it helps!!
salesforce_hoonigansalesforce_hoonigan
Wow. I don't know what happened, did not changed anything but it suddenly worked. Thanks Alex. 

ONE LAST thing. How can we add decimal in there? For example 01/10/2015 8:00 AM + 2.30 = Result should be 01/10/2015 10:30 AM
Alexander TsitsuraAlexander Tsitsura
Solution is very simple.
You need change 
integer.valueOf(pt.Hours__c) * 60 * 60 * 1000

on
integer.valueOf(pt.Hours__c * 60 * 60 * 1000)

Thanks,
Alex
salesforce_hoonigansalesforce_hoonigan
Thanks Alex! I appreciate the help. :D