TempBlob & Base64 with AL

Some of you guys might have noticed that our beloved record “Temp Blob” will soon vanish. It has been marked as deprecated for quite some time now and will be replaced by the new “blob storage module”. So, we’ll find them in codeunits for TempBlob & Base64.

What we get?

First, there’s the new codeunit “TempBlob”, which we could use to load, store, and transform streams from and to various sources. Such could be RecordRef, Record or Streams by themselves. The stream is then stored in this “storage module” for further processing.

procedure BlobStorage()
var
    lTempBlob: Codeunit "Temp Blob";
    lOutStream: OutStream;
    lInStream: InStream;
begin
    // Alternative #1 - Create an OutStream and write to Blob Storage Module
    TempBlob.CreateOutStream(lOutStream);
    lOutStream.WriteText('...');

    // Alternative #2 - Directly write i.e. stored logo in Field 15 to Blob Storage Module
    TempBlob.FromRecord(MyRec, 15);

    // Load the previously saved blob to an InStream for further processing
    TempBlob.CreateInStream(lInStream);

    //...
end;

This is a nice extension to work with streams over various sources. And especially the new methods to load streams i.e. from RecordRef to the “storage module” are real bonus value. But the ones that already checked this new codeunit will have realized the lack of Base64 methods that we were used to see with TempBlob.FromBase64() and TempBlob.ToBase64().

Never fear! We find them in the codeunit “Base64 Convert”!

Example for Base64 Conversion

We are querying a webservice and its response is a base64 encoded logo which we would like to store in a blob field of our record. “Base64 Convert” now allows us to handle this on diverse ways. Simple Text 2 Text conversions, or, in this case much more useful, an overloaded method to convert the base64 directly into the OutStream of the record.

//Create empty OutStream for our logo blob
MyRec.LogoBlob.CreateOutStream(lLogoOutStream);

//Directly convert the incoming base64 string into the OutStream
Base64Convert.FromBase64(lBase64String, lLogoOutStream);

And yes, for sure you could also convert an already existing file to Base64.

//Get InStream of our existing logo
MyRec.LogoBlob.CreateInStream(lLogoInStream);

//Convert it into a base64 encoded string
MyBase64String := Base64Convert.ToBase64(lLogoInStream);

Finally, there’s some overloads for conversion of text from and to Base64 which might be useful i.e. in Web Requests.

//Convert TextValue to Base64
Base64String := Base64Convert.ToBase64(OriginalString);

//Convert Base64 to TextValue
OriginalString := Base64Convert.FromBase64(Base64String);

... is a technical consultant and developer at Comsol Unternehmenslösungen AG in Kronberg/Taunus. Major tasks are the architecture and implementation of complex, usually cross-system applications in and around Microsoft Dynamics 365 Business Central.

Leave a Reply

Your email address will not be published.