Dynamics NAV Use a Date Time as a Numeric Code
I was working on some Dynamics NAV code recently where I was sending an XML file as a HTTP Request and receiving a response xml back from the Website, in the past I have worked with some systems where the sent file name had to be the same every time, if I had to export a file called Order.xml send this to a website, wait for a response, store the file and response with a matching key so the response could be processed in a batch how would I approach this.
I could use a number series which would mean adding a field in a setup, maintaining the lines and calling the No. Series Management Codeunit, I could use a custom format and a Datetime, the solution I think will work is to generate a numeric value between 10 – 13 Digits.
I do this by using two Datetime variables and calculating the number of milliseconds between the two dates, 13 Digits has a precision of 1 millisecond, 12 Digits 100th of a Second, 11 Digits 10th of a Second and 10 Digits has a 1 second precision, I would choose the best fit for the process I was writing, I could use a SLEEP(miliseconds) to allow for a unique value.
To test this theory I use a Processing only Report with no layout or sections, it will need one function and several variables, there are three ’C/AL Globals’ as in the screenshot, on the Functions I create a new Function called GetDateRef which is called with two parameters.
When the function is called two variables are passed in a Datetime to calculate with and an integer to trim the returned value.
In the GetDateRef function there are two Variables used for the calculation.
To display the results of the test code a C/AL Global Text Constant is also needed.
%1 %2 13 Digits – Thousandth of a Second\\%1 %3 12 Digits – Hundreth of a Second\\%1 %4 11 Digits – Tenth of a Second\\%1 %5 10 Digits – Whole Second
Code for the GetDateRef Function is simple, it creates a base date for the year 1900 and finds the number of milliseconds since 1900 to the passed in date.
What the Code Does:
- Check that a Date Time (ForDateTime) has been Passed In.
- If the Passed in Length is 0 then default it to 13.
- Create the Date Time (DT) at the 1st January 1900 at 00:00:00 am.
- Calculate the difference between the two Date Times as a Big Integer.
- Format the Date Time Difference and Trim to the Number of Digits the Length requested.
IF ForDateTime = 0DT THEN EXIT(''); IF Length = 0 THEN Length := 13; DT := CREATEDATETIME(01011900D,0T); Diff := ForDateTime - DT; EXIT(COPYSTR(FORMAT(Diff),1,Length));
OnPreReport a little code to test the function the dialogue windows shows the various date code strings that could be returned using the current Datetime and the maximum Datetime.
What the Code Does:
- Sets the Variable TempDT to the Current Date Time.
- Opens a Message Dialogue with the four different string Lengths
- Sets the Variable TempDT to 31st December 2199, to show it will work for a good while.
- Opens a Message Dialogue with the four different string Lengths
TempDT := CURRENTDATETIME; MESSAGE(Text001, 'Current Time', GetDateRef(TempDT,0), GetDateRef(TempDT,12), GetDateRef(TempDT,11), GetDateRef(TempDT,10) ); TempTime := 115959.999T; TempDate := 31122199D; TempDT := CREATEDATETIME(TempDate,TempTime); MESSAGE(Text001, 'End Time', GetDateRef(TempDT,0), GetDateRef(TempDT,12), GetDateRef(TempDT,11), GetDateRef(TempDT,10) );
on the 31st December 2199 23:59:59.999
That’s all good but how could it be used in the real world, using the scenario of a file called Order.xml which is being passed to a website and a response is returned, the request is generated and sent to the website the response is returned, a variable is used to get the string or the Current Date Time, the string is prefixed or appended to the Order and Response.
The response is opened and processed updating the Order or Sending a Failed email to the Sale Office, then the two files are Archived, here we can see a number of test files processed in the same minute different seconds using a length of 10 and a sleep to make sure that two files are not processed in the same second.
There is no download for this one, feel free to comment.