Sometimes, a string representation isn't enough. In order to perform certain actions, you need your data to be in a certain format. For example, we are reading data from a text file, so our entire data is simple text, which needs to be converted into an appropriate data type to use it in NAV. This recipe will show you how to change that data into a format that you can use.
Let's start by creating a new codeunit from Object Designer.
Now add the following global variables:
Name
Type
Length
DateText
Text
30
DateValue
Date
Write the following code into the
OnRun
trigger of the codeunit:DateText := '01/10/2012'; EVALUATE(DateValue, DateText); MESSAGE('Microsoft Dynamics NAV 2013 launch date is %1', DateValue);
To complete the development, save and close the codeunit.
On executing the codeunit, you should see a window similar to the one shown in the following screenshot:
The EVALUATE()
function takes in two parameters. The first is a variable of the type that we want our value to be converted into. This could be date, time, Boolean, integer, or any other simple data type. This parameter is passed by reference, meaning that the result of the function is stored in that variable. There is no need to do a manual assignment to get a return value.
The second parameter is the string that you need to convert. This text is usually stored in a field or variable, but can also be hardcoded.
The EVALUATE()
function is widely used in NAV C/AL code. The following code snippet is taken from the
CheckCreditCardData()
function of the 825
, Do Payment Mgt
codeunit:
EVALUATE (IntValue1,FORMAT(TODAY,0,'<Year>')); EVALUATE (IntValue2,COPYSTR(DOPaymentCreditCard."Expiry Date",3,2)); IF IntValue1 > IntValue2 THEN ERROR (Text006, CreditCardNo, DOPaymentCreditCard.FIELDCAPTION ("No."));
Before completing a transaction, the credit card's validity period needs to be checked. The preceding code extracts the year from the current date provided by the TODAY
function and the expiry date of the credit card. Both the values are evaluated using the relational operator. If the card has expired, the system will execute a predefined error message in text constant Text006
.