Tuesday, April 16, 2024

Calculate FromDate/ToDate based on Daily,Weelkly,Monthly in X++

boolean canRun = false;
TransDate fromDate, toDate, prevDate;
TransDate currentDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
switch (Reporting.Schedule)
{
	case ReportingSchedule::Daily :
		fromDate = currentDate - 1;
		toDate = currentDate - 1;
		canRun = true;
		break;
	case ReportingSchedule::Weekly :
		PreferredLocale preferredLocale = (new xSession()).PreferredLocale();
		prevDate = HcmDateTimeUtil::calculateDateWithOffset(PeriodUnit::Day,7,false, currentDate);
		fromDate = DateTimeUtil::getStartOfWeekDate(preferredLocale, prevDate);
		toDate   = DateTimeUtil::getEndOfWeekDate(preferredLocale, prevDate);
		canRun   = dayOfWk(currentDate) == 1; //1 for monday
		break;
	case ReportingSchedule::Monthly :
		prevDate = prevMth(currentDate);
		fromDate = DateStartMth(prevDate);
		toDate   = endmth(prevDate);
		canRun   = dayOfMth(currentDate) == 1; //1 for first day of the month
		break;
}

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8