CALCDATE

CALCDATE allows us to calculate a date value assigned to a Date data type variable. The calculation is based on a Date Expression applied to a Base Date (Reference Date). If we don't specify a BaseDateValue, the current system date is used as the default date. We can specify the BaseDateValue either in the form of a variable of data type Date or as a Date constant.

The syntax for CALCDATE is as follows:

DateVariable := CALCDATE ( DateExpression [, BaseDateValue])

There are a number of ways in which we can build a DateExpression. The rules for the CALCDATE function, DateExpression, are similar to the rules for DateFormula, which is described in Chapter 3, Data Types and Fields.

If there is a CW, CM, CP, CQ, or CY (Current Week, Current Month, Current Period, Current Quarter, or Current Year) parameter in an expression, then the result will be evaluated based on the BaseDateValue. If we have more than one of these in our expression, the results are unpredictable. Any such expression should be thoroughly tested before releasing to users.

If our Date Expression is stored in a DateFormula variable (or a Text or Code variable with the DateFormula property set to Yes), then the Date Expression will be language independent. Also, if we create our own Date Expression in the form of a string constant within our inline C/AL code, surrounding the constant with <> delimiters as part of the string, it will make the constant language independent. Otherwise, the Date Expression constant will be language dependent.

Regardless of how we have constructed our DateExpression, it is important to test it carefully and thoroughly before moving on. Incorrect syntax will result in a runtime error. One easy way to test it is by using a Report whose sole task is to evaluate our expression and display the result. If we want to try different Base Dates, we can use the Request Page, accept the BaseDate as input, then calculate and display the DateVariable in the OnValidate trigger.

Some sample CALCDATE expression evaluations are as follows:

  • ('<CM>',031017D) will yield 03/31/2017, that is, the last day of the Current Month for the date 3/10/2017
  • ('<-WD2>',031216D) will yield 03/08/2016, that is, the WeekDay #2 (the prior Tuesday) before the date 3/12/2016
  • ('<CM+1D>',BaseDate) where BaseDate equals 03/10/17, will yield 04/01/2017, that is, the last day of the month of the Base Date plus one day (the first day of the month following the Base Date)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.216