You need to sign in to do that
Don't have an account?
faceroy123
Time / Date trigger based on business hours
Hi,
I have the following code that I would like to populate a date / time field to 2 hours after task creation date (in business hours).
This works fine if i set this to after or before update. but i need it to work upon insert of a new task.
currently it fires this error:
"Review all error messages below to correct your data.
Apex trigger calcBusinessHours caused an unexpected exception, contact your administrator: calcBusinessHours: execution of BeforeInsert caused by: System.MathException: null: (System Code)"
trigger calcBusinessHours on Task (before insert) { BusinessHours stdBusinessHours = [select id from businesshours where id = '01m20000000LPVT']; for (Task ta : Trigger.new) { if ((ta.recordtypeid == '012M00000008bZY') && ta.status != 'completed' ){ // CL works 10 hours / day (8:00 AM - 18:00 PM, M-F). Our task SLA is 2 hours (2 business hours) ta.SLA_Due_Date__c = BusinessHours.addGmt (stdBusinessHours.id, ta.createddate, 2 * 60 * 60 * 1000L); } } }
Hi,
You can use system.today() instead of ta.createddate. Try the below code as reference:
trigger calcBusinessHours on Task (before insert) {
BusinessHours stdBusinessHours = [select id from businesshours limit 1];//where id = '01m20000000LPVT'];
for (Task ta : Trigger.new) {
if (ta.status != 'completed' ){
// CL works 10 hours / day (8:00 AM - 18:00 PM, M-F). Our task SLA is 2 hours (2 business hours)
ta.activitydate = date.valueof(BusinessHours.addGmt (stdBusinessHours.id, system.today(), 2 * 60 * 60 * 1000L));
}
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
All Answers
Hi,
You can use system.today() instead of ta.createddate. Try the below code as reference:
trigger calcBusinessHours on Task (before insert) {
BusinessHours stdBusinessHours = [select id from businesshours limit 1];//where id = '01m20000000LPVT'];
for (Task ta : Trigger.new) {
if (ta.status != 'completed' ){
// CL works 10 hours / day (8:00 AM - 18:00 PM, M-F). Our task SLA is 2 hours (2 business hours)
ta.activitydate = date.valueof(BusinessHours.addGmt (stdBusinessHours.id, system.today(), 2 * 60 * 60 * 1000L));
}
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
I used System.now() which worked fine.
Thanks
Navatar_DbSup Thank you for your post. Did you also notice that instead of SOQLing standard business hours by id or by limiting to 1 you can SOQL as follows: BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true]; ? Just located that in the Apex Code Developer's Guide here: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_classes_businesshours.htm|StartTopic=Content%2Fapex_classes_businesshours.htm|SkinName=webhelp