If you ever had to read the notes blob in the Record Link table, you might have noticed that it contains some strange characters. This makes it hard to use anywhere without some handling.
This is because NAV uses .NET BinaryWriter and writes the length of the string in it.
If you look around the internet, you will find some code to actually do the writing. Some with .net dll’s, other with plain NAV code (http://www.sbsintellect.co.za/index.php/2012/03/custom-automated-notes-in-dynamics-nav-rtc-2/).
When you look to read the data, not much is found. And if you do find something, mostly external dll’s are used. So I took a bit of time to translate this into C/AL code.
Start by creating a new function which accepts 1 parameter and returns 1 variable:- Input: VAR RecordLink => Record “Record Link”
– Output: Text 1024
Also create the following variables locally:
– BinaryReader => DotNet ‘mscorlib, Version=18.104.22.168, Culture=neutral, PublicKeyToken=b77a5c561934e089’.System.IO.BinaryReader
– MemoryStream => DotNet ‘mscorlib, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b77a5c561934e089’.System.IO.MemoryStream
– InStream => InStream
Now you are basically halfway of reading the note string…
All that is left, is to make our function return the value which is done with the code below:
RecordLink.CALCFIELDS(Note); IF NOT RecordLink.Note.HASVALUE THEN EXIT(''); RecordLink.Note.CREATEINSTREAM(Instream); MemoryStream := MemoryStream.MemoryStream; MemoryStream := Instream; BinaryReader := BinaryReader.BinaryReader(MemoryStream); EXIT(COPYSTR(BinaryReader.ReadString(),1,1024));
There you go, really easy.
You will see that I am using a MemoryStream Object, that is because the BinaryReader will not accept the Instream as a variable, where according to the link to write, the BinaryWriter DOES accept the OutStream.
My code only reads the first 1024 characters. But I assume most of you are proficient enough to edit the code and read the note into an array, or use multiple functions to read the rest of the data.
Anyway, enjoy the code 🙂
Don’t forget that since this is using DotNet variables, it will only work through RTC. But it will also work for printing notes in RDLC Reports.