2

Here is what I am attempting to do. I have a date on a custom object Deal__c. The date is Asset Management Close Date (Asset_Mgmt_Close_Date__c).

The sidecar__c object is created with a master-detail relationship to the Deal__c object. When that happens the trigger fires and populates the Funding Date Date_Funded__c field on the sidecar__c object. I then take the sidecar funding date and add various numbers of business days to it and at the end of the trigger I am attempting to take that Funding Date and first add 30 calendar days and then second add 1 business day.

I have tried several different ways of doing this with inconsistent results. I sometimes get an additional day added, so two days instead of one. Sometimes no days get added but some hours. I am totally stumped. Nothing I try works 100% of the time. Any help would be greatly appreciated. I'm sure it has to do with the milliseconds thing - but I cannot figure it out.

trigger UpdateFundingDate2 on Sidecar__c(before insert, before update) {
    Set < Id > dealIds = new Set < Id > ();
    BusinessHours bh1 = [SELECT Id FROM BusinessHours WHERE Id = '01m0q0000008Z6B'];
    BusinessHours bh2 = [SELECT Id FROM BusinessHours WHERE Id = '01m0q0000008Z6G'];
    Long a7milliSec = 1 * 24 * 60 * 60 * 1000;
    System.debug('LONG VALUE :: ' + a7milliSec);

    for (Sidecar__c sc: Trigger.new) {
        if (sc.Deal__c != null) {
            dealIds.add(sc.Deal__c);
        }
    }

    Map < Id, Deal__c > dealEntries = new Map < Id, Deal__c > (
        [select Asset_Mgmt_Close_Date__c, CFC_Mandate_Date__c from Deal__c where id in: dealIds]
    );

    for (Sidecar__c sc: Trigger.new) {
        Deal__c d = dealEntries.get(sc.Deal__c);

        if (d != null) {
            sc.Date_Funded__c = d.Asset_Mgmt_Close_Date__c;
            sc.CFC_Mandate_Date__c = d.CFC_Mandate_Date__c;

            datetime df = sc.Date_Funded__c;
            date df1 = df.dateGMT();
            system.debug('check if Date Funded is in GMT:' + df);
            system.debug('check if Date Funded is in GMT:' + df1);

        }
    }
    for (Sidecar__c sc: Trigger.new) {

        DateTime dT1 = BusinessHours.add(bh1.Id, sc.Date_Funded__c, -7 L * 23 * 59 * 60 * 1000);
        // Date myDate1 = date.newinstance(dT1.year(), dT1.month(), dT1.day());
        sc.X7_bd_before_close__c = dT1.date();
        System.debug('BD7CALC :: ' + dT1);
        // System.debug('BD7CALCa :: '+myDate1);
    }
    for (Sidecar__c sc: Trigger.new) {

        DateTime dT2 = BusinessHours.add(bh1.Id, sc.Date_Funded__c, -6 L * 23 * 59 * 60 * 1000);
        // Date myDate2 = date.newinstance(dT2.year(), dT2.month(), dT2.day());
        sc.X6_BD_before_close_US__c = dT2.date();
        System.debug('BD6CALC :: ' + dT2);
        // System.debug('BD6CALCa :: '+myDate2);
    }
    for (Sidecar__c sc: Trigger.new) {

        DateTime dT3 = BusinessHours.add(bh2.Id, sc.Date_Funded__c, -6 L * 23 * 59 * 60 * 1000);
        // Date myDate3 = date.newinstance(dT3.year(), dT3.month(), dT3.day());
        sc.X6_BD_before_close_US_IR__c = dT3.date();
        System.debug('BD6CALCI :: ' + dT3);
        // System.debug('BD6CALCIa :: '+myDate3);
    }
    for (Sidecar__c sc: Trigger.new) {

        Date dta = sc.Date_Funded__c;
        Date newdta = dta.addDays(30);
        DateTime dT4 = BusinessHours.add(bh1.Id, newdta, +1 L * 17 * 60 * 60 * 1000);
        // Date myDate4 = date.newinstance(dT4.year(), dT4.month(), dT4.day());
        sc.HFS_30_1_BD__c = dT4.date();
        System.debug('BD30CALCa :: ' + newdta);
        System.debug('BD30CALCb :: ' + dT4);
        // System.debug('BD30CALCc :: '+myDate4);
    }
    for (Sidecar__c sc: Trigger.new) {

        Date dtb = sc.Date_Funded__c;
        Date newdtb = dtb.addDays(45);
        DateTime dT5 = BusinessHours.add(bh1.Id, newdtb, +1 L * 24 * 60 * 60 * 1000);
        // Date myDate5 = date.newinstance(dT5.year(), dT5.month(), dT5.day());
        sc.HFS_45_1_BD__c = dT5.date();
        System.debug('BD45CALCa :: ' + newdtb);
        System.debug('BD45CALCb :: ' + dT5);
        // System.debug('BD45CALCc :: '+myDate5);
    }
}

one example:

08:26:41.920 (927226590)|USER_DEBUG|[32]|DEBUG|check if Date Funded is in GMT:2018-12-01 00:00:00
08:26:41.920 (927243439)|USER_DEBUG|[33]|DEBUG|check if Date Funded is in GMT:2018-12-01 00:00:00
08:26:41.920 (928092728)|USER_DEBUG|[43]|DEBUG|BD7CALC :: 2018-11-22 09:41:00
08:26:41.920 (928308895)|USER_DEBUG|[52]|DEBUG|BD6CALC :: 2018-11-23 08:18:00
08:26:41.920 (928690369)|USER_DEBUG|[61]|DEBUG|BD6CALCI :: 2018-11-23 08:18:00
08:26:41.920 (928879034)|USER_DEBUG|[72]|DEBUG|BD30CALCa :: 2018-12-31 00:00:00
08:26:41.920 (928892513)|USER_DEBUG|[73]|DEBUG|BD30CALCb :: 2018-12-31 23:00:00
08:26:41.920 (929055964)|USER_DEBUG|[84]|DEBUG|BD45CALCa :: 2019-01-15 00:00:00
08:26:41.920 (929068582)|USER_DEBUG|[85]|DEBUG|BD45CALCb :: 2019-01-16 00:00:00

1 Answer 1

1

First, add 30 the calendar days to a given date.

Date dta = System.today().addDays(30);

Then add 1 Business Day. Here is the catch, if you consider 8 hours per day (SLA type) then use like this.

DateTime dt =  BusinessHours.add(defaultBH.Id,dta, 8* 60 * 60 * 1000L);

If you want to test this properly, match the user's timezone with the Business Hours timezone. After initial testing, you should change timezone as per your requirement.

You can also see additional days might get added if that DateTime is outside of Business Hours.

To test that above resultant datetime correctly, make Business Hours as 24 hours, including Holidays and then try to setup actual business hours which is required for your org.

9
  • Thank you @Santanu. I made that change and verified my business hours are 24 hours M-F and no hours Sat and Sun with no holidays set up yet. using an Asset Mgmt Close Date of 11/30 I get the following:09:31:03.127 (1138192407)|USER_DEBUG|[72]|DEBUG|BD30CALCa :: 2018-12-30 00:00:00 09:31:03.127 (1138217243)|USER_DEBUG|[73]|DEBUG|BD30CALCb :: 2019-01-01 06:00:00 Code is now Date dta = sc.Date_Funded__c; Date newdta = dta.addDays(30); DateTime dT4 = BusinessHours.add(bh1.Id, newdta, 24*60*60*1000); how is 2018-12-30 00:00:00 + 24 hours = 2019-01-01 06:00:00? what am i missing?
    – ZinniaGirl
    Commented Nov 9, 2018 at 15:44
  • Oh - I see. The 6 extra hours are due to my timezone being GMT+6 :) ok. I think then changing my business hours timezone to GMT fixes it? Since I have 24 hours each day I don't think I care about my timezone. But if I do need my timezone to be GMT+6 then what do I do in my trigger - subtract 6 hours?
    – ZinniaGirl
    Commented Nov 9, 2018 at 15:51
  • Ok - I think I may at least understand the challenge now. So changing my business hours to GMT resolved the 6 hour issue. Now what is happening is this - when the funding date + 30 calendar days ends up on a weekend then that result is - as an example let's say Sunday 2018-12-16 00:00:00 - then adding 24*60*60*1000 ends up 2 business days later at 2018-12-18 00:00:00
    – ZinniaGirl
    Commented Nov 9, 2018 at 16:23
  • After further testing - this is exactly the issue. when the date + 30 ends up on a weekend then the result after adding business days is a day off.
    – ZinniaGirl
    Commented Nov 9, 2018 at 16:29
  • thinking maybe I need an if statement that checks if the date of the result of adding 30 days is a weekend or holiday then I add less time and if it is a business day then I can use the businessHours.add
    – ZinniaGirl
    Commented Nov 9, 2018 at 17:06

You must log in to answer this question.

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