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

How to: Remove/Restore Departments Button

We all know that we can remove the Departments button from NAV Window Client (RTC). This subject was discussed many times on other blogs.

Here is an excellent post that describes this topic: https://saurav-nav.blogspot.fr/2013/04/configuring-profile-in-role-tailored.html

But what if you need to restore the Departments button? The answer you can get on internet is: Go to configuration mode and restore the profile to default. Unfortunately, if you apply this solution, you will lose all the personalization you already did (how many hours of hard working? ;))

Today, I will explain to you how you can restore the Departments button without losing your personalization.

The solution consists of few simple steps:

  1. Export the profile using Xmlport “Profile Import/Export” (ID 9170) and filter to your PROFILEID (or the action “Export Profile” on Profile Card/List for NAV 2017).
  2. Open the exported profile (xml file) with your favorite text editor (notepad++ is my favorite),
  3. Look for the value: Attribute value=”Departments”,
  4. Select and delete the node that contains this attribute (see image below),
  5. Save the modified xml file,
  6. Delete you profile from NAV,
  7. Import the modified xml file to NAV,
  8. Check and enjoy your “restored” Departments button.
How to_ Remove_Restore Departments Button

P.S: I tested this on NAV 2013, NAV 2013 R2, NAV 2015, NAV 2016 and NAV 2017. Apparently, Microsoft uses a unique ID for the Departments button {7AA351B2-CC3E-479f-9A0B-6BC0DD7A3158}. You may also use this ID to find the correct xml node.

This blog is available also on Microsoft Dynamics NAV Community

How to calculate Dimension Set ID

Starting from Dynamics NAV 2013, dimension’s structure has changed a lot. Since then, a lot of developers struggle to insert or update dimensions pragmatically.

I will not bother you with what was already explained a hundred times in other blogs on how to calculate Dimension Set ID. Better, I will share with you a simple codeunit “SpecDimensionManagement” that will save you time whenever you will need to update dimensions (by update I mean Create, Update, Delete).

In order to use my codeunit, I invite you respect the following example:

WITH SpecDimensionManagement DO BEGIN
// Get Current Dimension Set ID or init a new one
INIT(MyRecord);
// Update my dimensions values
UPDATE(‘DIM1’, ‘VALUE1’);
UPDATE(‘DIM2’, ‘VALUE2’);
UPDATE(‘DIM3’, ‘VALUE3’);
DELETE(‘DIM4’);
DELETE(‘DIM5’);
DELETE(‘DIM6’);
// Calculate my new Dimension Set ID
MyRecord.”Dimension Set ID” := GetDimSetID;
// Get new values if needed
GetCurrDimValues(DimCode, DimValueCode, MyRecord.”Dimension Set ID”);
END;

I put some comments to explain what the functions do.

In the fob, you’ll find codeunit 92600 that handle the calculation and codeunit 92601 where I create some examples for test.

Feel free to contact me if you need more details / informations. Share your ideas and feedback in comment section 🙂

P.S: the fob is from a NAV 2015 FR (Build 49000) database. I test it on NAV 2017 and 2016 also.

This blog is available also on Microsoft Dynamics NAV Community

Print to different trays in NAV – Part 4

This post is part of a series of posts where I explain how to print to different trays in NAV.

  1. Print to different trays in NAV – Part 1
  2. Print to different trays in NAV – Part 2
  3. Print to different trays in NAV – Part 3
  4. Print to different trays in NAV – Part 4

In this forth and last part, we’ll simply print a sales invoice respecting the following requirements:

  1. The first page should print from first tray where we will use a colored paper,
  2. The last page should print from second tray where we will use preprinted paper where we can read general terms and conditions in the back,
  3. Other pages should print from the lower tray for example.

To fulfill this requirement, we’ll use the options already described in Part 1.

For test purpose, I will save report “Sales – Invoice” (ID 206) under a new name and number. I’ll adjust properties as below:

  • PaperSourceFirstPage  = Custom 2,
  • PaperSourceDefaultPage = Lower,
  • PaperSourceLastPage = Custom 3.

Then I’ll create an invoice, post, and print it. VoilĂ  !

Sources and examples are available here.

That’s great. But when you print multiple invoices or when you print multiple copies of your invoice, you’ll quickly notice that NAV does not pages in the right trays.

Unfortunately, this is a limitation in Windows Client / RTC because:

  • RDLC does not support PAPERSOURCE,
  • RDLC uses report properties for physical pages only.

Of course, this limitation is not relevant for NAV 2009 R2 Classic Client and older versions because you can force PAPERSOURCE in the code.

But hey, you should upgrade your NAV if you still use NAV 2009 R2 or older. So, you’ll face these limitations soon 😀

This was the last part of this blog post series. I hope it will help someone. Please share your ideas or just say hello if you like my work.


This blog is available also on Microsoft Dynamics NAV Community

Print to different trays in NAV – Part 3

This post is part of a series of posts where I explain how to print to different trays in NAV.

  1. Print to different trays in NAV – Part 1
  2. Print to different trays in NAV – Part 2
  3. Print to different trays in NAV – Part 3
  4. Print to different trays in NAV – Part 4

In this third part, we’ll see how you can find what are available trays in your printer.

To do this, I will explain two solutions.

Solution 1:

This solution is based on a simple tool called “FindTrays”. This tool was kindly shared by Timo LĂ€sser from German NAV community msdynamics.de. You can directly download FindTrays from this link.

You can get available trays by executing this command line:

.\FindTrays.exe “MyPrinterName” > .\AvailableTrays.txt

How do you get your printer’s name? Simple, just go to your printer properties and you’ll find it under the general tab.

Solution 2:

The second solution I suggest is based on NAV. The idea is to create a simple report that prints the tray number using:

  • PAPERSOURCE function for NAV 2009 R2 Classic Client and older version,
  • Properties PaperSourceFirstPage, PaperSourceDefaultPage and PaperSourceLastPage for version with Windows Client / RTC (i.e. NAV 2013 and later).

Sources and examples are available for download here. Enjoy!

In the fourth part, we’ll apply what we learned, and we’ll discuss some NAV limitations.


This blog is available also on Microsoft Dynamics NAV Community

Print to different trays in NAV – Part 2

This post is part of a series of posts where I explain how to print to different trays in NAV.

  1. Print to different trays in NAV – Part 1
  2. Print to different trays in NAV – Part 2
  3. Print to different trays in NAV – Part 3
  4. Print to different trays in NAV – Part 4

In this second part, we’ll see how you can test your printing in different trays.

If you need to test your printing you will, of course, need a printer that support printing in different trays. But what if you don’t have such a printer? Or, what if you -like me- hate to waste paper on your tests?

The answer is Bullzip PDF printer. In fact, this proprietary PDF virtual printer allows to add virtual trays. I’ll let you read complete explanation in Bullzip Knowledge Base.

Just in case the link is down, I’ll quickly explain the steps hereafter. All you need to do is:

  1. Download and install the virtual printer with default parameters (adjust if needed),
  2. Open, as admin, the “BULLZIP.PPD” file located in “C:\Windows\System32\spool\drivers\x64\3\“,
  3. Add the lines below to the end of the file and save it:

*InputSlot Tray1/Tray 1: “gsave /Times-Roman findfont 30 scalefont setfont newpath 10 10 moveto 1 0 0 setrgbcolor (Tray 1) show grestore”
*InputSlot Tray2/Tray 2: “gsave /Times-Roman findfont 30 scalefont setfont newpath 10 10 moveto 1 0 0 setrgbcolor (Tray 2) show grestore”
*InputSlot Tray3/Tray 3: “gsave /Times-Roman findfont 30 scalefont setfont newpath 10 10 moveto 1 0 0 setrgbcolor (Tray 3) show grestore”
*InputSlot Tray4/Tray 4: “gsave /Times-Roman findfont 30 scalefont setfont newpath 10 10 moveto 1 0 0 setrgbcolor (Tray 4) show grestore”
*InputSlot Tray5/Tray 5: “gsave /Times-Roman findfont 30 scalefont setfont newpath 10 10 moveto 1 0 0 setrgbcolor (Tray 5) show grestore”

You can download the file I created here (delete .doc from the extension).

Now that the printer is ready, let’s see how you can find out what are the available trays in your printer. The answer is in part 3.

P.S 1: this blog is primarily addressed for NAV developers.

P.S 2: this blog could be of use for none-NAV developers also as it explains a solution to create virtual printer with different trays.

P.S 3: The same trick works also for PDFCreator (i.e. add virtual trays to the virtual printer). So, you can try it with your preferred virtual printer. The PPD file is available in the same location “C:\Windows\System32\spool\drivers\x64\3\PDFCREAT.PPD”.


This blog is available also on Microsoft Dynamics NAV Community

Print to different trays in NAV – Part 1

This post is part of a series of posts where I explain how to print to different trays in NAV.

  1. Print to different trays in NAV – Part 1
  2. Print to different trays in NAV – Part 2
  3. Print to different trays in NAV – Part 3
  4. Print to different trays in NAV – Part 4

In this first part, we’ll quickly talk about options available in NAV that allow printing to different trays.

First, let’s check properties of report “Sales – Invoice” (ID 206):

ReportProperties.jpg

As you can see there are three properties that will allow you select the tray where the page should print. Below, you can read help from MSDN:

  1. PaperSourceFirstPage Property
  2. PaperSourceDefaultPage Property
  3. PaperSourceLastPage Property

Available values for each property are described here: Paper Sources and Tray Numbers This link refers to “Norway Local Functionality”, though it explains all available trays numbers in any NAV database (W1 and Localizations).

P.S: In NAV 2009 R2 and older versions, the available options are PaperSourceFirstPage and PaperSourceOtherPages.

In the next blog, I will explain how you can try those options without having a printer with different trays.


This blog is available also on Microsoft Dynamics NAV Community

Text encoding in NAV 2009 R2 Classic Client – Part 2

As I already explained in this blog post you can import text files with encoding in Dynamics NAV 2009 R2 Classic Client and old versions.

Today, I will use the same trick to export a text file with encoding (UTF-8 in the example below).

To illustrate this, let’s export all items in a text file from a Russian database. I’ll use three possible solutions:

1- Via dataport:

Dataport-Export Item.jpg

2- Via codeunit using variable of type File:

Codeunit File-Export Item.jpg

3- Via codeunit using variable of type Automation (Stream):

Codeunit Stream-Export Item.jpg

Now, let’s see the results:

Result.jpg

We quickly notice that export via dataport or via File variable does not do the job as Russian characters are not exported correctly whereas export via Stream does the job perfectly.

As always, I let you download the objects and files I used in this blog post.

This blog is available also on Microsoft Dynamics NAV Community

Text encoding in NAV 2009 R2 Classic Client – Part 1

P.S: If you’re still using Microsoft Dynamics NAV 2009 R2 Classic Client or a previous version, then you may want to continue reading this blog post.

We all know that Microsoft Dynamics NAV 2009 R2 Classis Client supports ANSI only (code page 1252 or depends on your windows localization).

In order to import/export encoded files we usually use some workarounds. In fact, when you google (bing) you will find two main answers:

  • Ansi-Ascii converter: but you need to add new characters in the codeunit as they come up,
  • Use some third party tools: but this will create dependency and it will cause a headache to maintain your NAV platform.

The solution I personally prefer is to use “Microsoft ActiveX Data Objects 2.8 Library” automation. In fact, this Dll is available on every windows machine and it offers a an interesting object: Stream.

In order to use it, you need to:

  1. Know your text file encoding,
  2. Check that your OS supports the needed language,
  3. Use a simple code is your NAV:

CREATE(Stream);
Stream.Open;
Stream.Charset(_Charset);
Stream.LoadFromFile(_Path);
Line := Stream.ReadText(1024);
Stream.Close;

Let’s see how this works concretely. In the example below, I will use a Russian Windows Server (change English to Russian):

RegionAndLanguage

Then, I will use two Russian text files with different encoding: UTF-8 and IBM855 (OEM 855). Finally, I test all on a NAV 2009 R2 Russian Native Demo Database.

CronusRU

I’ll let you see the results:

Below some comments to understand the results:

CodeComment
FIL_IBM855Read a IBM855 encoded file with File variable
FIL_UTF8Read a UTF-8 encoded file with File variable
IBM_UTFRead a IBM855 encoded file with Stream automation and wrong Charset = UTF-8
STR_IBM855Read a IBM855 encoded file with Stream automation and correct Charset = IBM855
STR_UTF8Read a UTF-8 encoded file with Stream automation and correct Charset = UTF-8
UTF_IBMRead a UTF-8 encoded file with Stream automation and wrong Charset = IBM855

Finally, I’ll let you download the objects and the files I used in the examples here.

This blog is available also on Microsoft Dynamics NAV Community

My two cents about Try/Catch in C/AL

By introducing NAV 2016, Microsoft introduced Try Functions to endow developers with some sort of Try/Catch in C/AL.

The purpose of this blog is not to add another entry to explain how to use Try Functions.  has already explained this here. The problem with these types of functions is resumed in this sentence from MSDN:

Changes to the database that are made with a try function are not rolled back.

So, inattentive developers may cause some calamities… You know what I mean 😉

Fortunately, Microsoft caught up by the introduction of NAV 2017. It added a new parameter DisableWriteInsideTryFunctions. It is fully documented here.

Okay, this is all good to know but what is the point behind this post?

To answer this question, I will first ask a question 🙂

How can we create a “Try Function” for older versions (5.0 to 2015)?

The answer is simple, and every NAV developer know it (should know it):

IF MyCodeunit.RUN THEN
MESSAGE(‘OK’) // Continue processing…
ELSE
MESSAGE(GETLASTERRORTEXT); // Log the error or display it…

One of the biggest advantages drawbacks to me JosĂ© is not having to use one new codeunit everytime we need to “trycatch” a process. This can end up consuming a lot of codeunits in some cases. (Yes, I quoted JosĂ© and I made some modifications 🙂 ).

Here comes the purpose of my post. I would like to share with you a trick I used to use far away before NAV 2016. In fact, I created a Codeunit to centralize all processes that need a “trycatch”. It’s all based on this simple code (of course):

IF TryCatch.RUN(TempParam) THEN
MESSAGE(‘OK’)
ELSE
MESSAGE(GETLASTERRORTEXT);

“TryCatch” is my Codeunit where all the magic happens thanks to my temporary table “TempParam”. All you need to do; is to implement the function you want to “trycatch” inside the “Try/Catch” Codeunit and use it as I show in the “Try/Catch Example” Codeunit. Enough explanations, I believe the Codeunit is simple to understand. I’ll let you try and see by yourselves, download the objects here (fob and txt are from a NAV 2009 R2 database).

Of course, I’ll be glad to answer your questions if needed.

If “TempParam” trick does not suit your need then create your own solution (share it as comment if you want). You can for example create global variables inside “Try/Catch” Codeunit and initialize them by calling a function “InitMyGlobals” before you call TryCatch.RUN.

P.S.: In 2014, Vjeko explained why some sort of Try/Catch may be a big disaster for C/AL world (or at least, if not used wisely).

This blog is available also on Microsoft Dynamics NAV Community
« Older posts

© 2023 NAV NAB BLOG

Theme by Anders NorenUp ↑