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