Sometimes a string representation isn't enough. In order to perform certain actions, you need your data to be in a certain format. This recipe will show you how to change that data into a format that you can use.
Create a new codeunit from Object Designer.
Add the following global variables:
Name
Type
Length
DateText
Text
30
DateValue
Date
Write the following code in the
OnRun
trigger:DateText := '01/01/2010'; EVALUATE(DateValue, DateText); MESSAGE('Date: %1', DateValue);
Save and close the codeunit.
When you run the codeunit you should see a window similar to the following screenshot:
The EVALUATE()
function takes in two parameters. The first is a variable of the type that you want your 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 assign using the :=
syntax.
The second parameter is the string which you need to convert. This text is usually stored in a field or variable, but can also be hard coded.
For a list of all of the functions related to text variables, search for "Text Data Type" in the C/SIDE Reference Guide under the Help menu.
EVALUATE()
returns a boolean value when executed. If the conversion is successful, it returns TRUE
or 1
; otherwise, it returns FALSE
or 0
. If the function returns FALSE
, an error will be generated. If you wish to display the standard system error, you can leave the code as it is, but if you want to handle the error yourself, you must make the following changes:
DateText := '01/01/2010'; IF NOT EVALUATE(DateValue, DateText) THEN ERROR('Custom Error Message'); MESSAGE('Date: %1', DateValue);
Number series are used throughout the NAV system. Every document has a unique identifier that is usually retrieved from the No. Series table. This table keeps a track of the last number used so that it knows what the next number should be.
However, this identifier is not just a number. A purchase order, for example, might have an identifier of PO123456, which means that it is actually a string. As you can't add a number to a string, you will have to figure out what the number part is, convert it to an actual number, and then increment it. This code from the IncrementNoText()
function in codeunit 396, NoSeriesManagement, does exactly that. As this code calls several other functions, it may be beneficial for you to look through the entire codeunit.
GetIntegerPos(No,StartPos,EndPos); stringnumber series, incrementingEVALUATE(DecimalNo,COPYSTR(No,StartPos,EndPos - StartPos + 1)); NewNo := FORMAT(DecimalNo + IncrementByNo,0,1); ReplaceNoText(No,NewNo,0,StartPos,EndPos);