How to: Access timestamp from C/AL

How to access timestamp from C/AL? When you Google/Bing this question, you mainly get three answers:

  1. Short answer: you can’t,
  2. Write a SQL query using ADO.NET,
  3. Create a SQL view and use a linked table on NAV,

When Microsoft introduced NAV 2016, we got a fourth answer described in this blog post by Dynamics NAV Team Blog.

Here is my point of view about these solutions:

  1. Solutions 1: is not a solution,
  2. Solution 2: is like you use a hammer to kill a mosquito,
  3. Solution 3: well, this is not a solution too because:
    • You have to create a view on your database and this will give some headache when you upgrade…
    • You have to consume a table: what if you want to have the timestamp for records from two/three/N tables ? Would you create N dummy views and N linked tables? If it was me, the answer is a big NO.
  4. Solution 4: these is a great and useful improvement but:
    • You may have to customize standard tables (add new field of type BigInteger and set property “SQL Timestamp” to Yes)… Yet another customized table you may need to handle during your upgrades 😦
    • What about older versions?

After this short introduction (short?), I get right to the point “here is a fifth solution in three lines of code“:

IF NOT DataTypeManagement.GetRecordRef(_RelatedRecord, RecRef) THEN
EXIT(0);

EVALUATE(timestamp, FORMAT(RecRef.FIELD(0).VALUE));

Want to try it? Here is the fob for Dynamics NAV 2013. In this fob, the function GetTimestamp returns the decimal value for the record’s timestamp.

This solution works for Dynamics NAV 2013 and later. Unfortunately, it does not work for Dynamics NAV 2009 R2 or older.

This blog is available also on Microsoft Dynamics NAV Community

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s