1

I found an Apex trigger that adds 9 business hours, and I'm looking to use it to add 16 business hours to the CreatedDate field on a case and put that new value into a custom Datetime field called Qualification_Due_Date__c, and I'm having issues making it do what I want it to do. Any help would be greatly appreciated.

Here's the code as it currently sits...

trigger CalculateQualificationDueDate on Case (before insert, before update) {
    BusinessHours bh = [select Id from BusinessHours where IsDefault=true];
    Case.Qualification_Due_Date__c = BusinessHours.add(bh.Id, Datetime.Now (), 32400000);
}
6
  • Your code looks good to me. What exactly are the issues you are having?
    – dBeltowski
    Jun 8, 2015 at 15:09
  • When a case is created or edited it is not adding any values into the Qualification_Due_Date__c field, and I thought that things looked okay, so I'm at a loss. Relatively new to Apex, so it could be a really simple mistake being caused by my inexperience.
    – cazam.km
    Jun 8, 2015 at 15:13
  • What does your debug log show? Is BusinessHours being successfully queried? Is the .add function returning the value as expected? Is your Qualification_Due_Date__c populated as expected? Is there perhaps some other code or workflow updates that might clear out that field after it's been set in the trigger?
    – dBeltowski
    Jun 8, 2015 at 15:19
  • When I run the execute anonymous window, and have it create a new case, then return the qualification due date in the debug log, it get a value of Null.
    – cazam.km
    Jun 8, 2015 at 15:28
  • And I don't have any workflows or anything that would be clearing out the value after it is set.
    – cazam.km
    Jun 8, 2015 at 15:29

2 Answers 2

1

Try this code out and see if it works for you.

trigger CalculateQualificationDueDate on Case (before insert, before update) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    for( Case c : Trigger.new )
    {
        c.Qualification_Due_Date__c = BusinessHours.add(bh.Id, Datetime.Now (), 32400000);  
    }    
}
3
  • That did exactly what I was looking for!! Thank you!!
    – cazam.km
    Jun 8, 2015 at 15:52
  • @cazam.km Remember when writing triggers you need to use the "Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap" methods to actually access the sObjects in the trigger. I suggest you look into how those work, and also read up on Trigger Bulkification. Happy I could help.
    – dBeltowski
    Jun 8, 2015 at 15:57
  • thank you for the tip, that is exactly what I'll do this afternoon.
    – cazam.km
    Jun 8, 2015 at 16:08
0

try below code : updated from 32400000(9*3600000) to 57600000(12*3600000)

 trigger CalculateQualificationDueDate on Case (before insert, before update) {
    BusinessHours bh = [select Id from BusinessHours where IsDefault=true];
    Case.Qualification_Due_Date__c = BusinessHours.add(bh.Id, Datetime.Now (), 57600000);
}
1
  • Changing the length of time (in milliseconds) from 32400000 to 57600000 was one of my initial changes but I wanted to show the trigger as it originally appeared in the other post with minimal edits.
    – cazam.km
    Jun 8, 2015 at 15:18

You must log in to answer this question.

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