Using BusinessHours.add for calculating a due date in a Datetime field

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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);  
    }    
}

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x