Creating a "Date Created" Field

Hey guys,

My company wants me to create a “date created” field in the Item table so we can view when items were added to NAV using the RTC. I realize SQL Server itself has a timestamp, but that data is not viewable in RTC to my knowledge.

I went ahead and made a “Date Created” field set as type Date. However, I know there will need to be C/AL code involved to actually populate this field with the current date upon creation of a new record. Note that since this is “date created” and not “date modified”, I want it to insert the current date ONLY if it’s a new record - not every time someone opens the Page.

If the C/AL code needs to be created on the Page itself, that’s also doable.

I just do not know the code required to do this. I have a similar function added elsewhere for DateModified, but would like to know how I do what I’m trying to do. Thanks

To follow NAV “best practices” the right place to put the code that populates the new DateCreated field is Item table itself, in OnInsert trigger, NOT the page.

It is not SQL Server’s timestamp, it’s a special NAV field for maintaining multiuser data change functionality, called Optimistic Concurrency, inherited from Navision Native database.

I’ve got no problems putting it on the OnInsert trigger.

But I literally know nothing about C/AL syntax and a search for populating a date field with the current date didn’t really return anything useful.

Can someone give me a code snippet I can paste in there? It should be pretty universal as far as I know.

Write

“Date Created” := WORKDATE;

True, but if the requirement really is:

… it should be put on the page where the item is created. This most probably will be page 30 (Item Card). Then put this code:

In the OnInsertRecord on page 30.

Note

If you put the code on page 30, then only when creating an item using this page the “Date Created” field will be populated; in all other cases this will not happen and the field will remain empty.

“All other cases” can be items that are created through code or through other pages. In standard NAV these will hardly happen, but I do not know what (local) version of NAV you are using and whether you have customizations on your installation

Great, that worked. Thanks!

I figured it was something simple [:D]