You need to sign in to do that
Don't have an account?
salesforce_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:
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)); } } }
All Answers
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.
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
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
Still doesn't work.
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
My Timezone is set correctly, regardless of any value entered in Hours__c , it results to 01/10/2015 7:00 AM
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
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
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
You need change
on
Thanks,
Alex