Dynamics 365 Business Central: parsing a CSV file using CSV Buffer

A question received today was the following: is it possible from my extension in Dynamics 365 Business Central asking a user to select a local CSV file, load it and then managing the content accordingly to my business needs using CSV Buffer table?

In standard NAV code (C/AL) you can use CSV Buffer table to load a CSV file (without using an XMLPort) and then parsing its content line by line. You can load a CSV file by using the following C/AL code:

CSVBuffer.LoadData(PathOfYourCSVFile,Separator);

And what about AL code? Can I use CSV Buffer in my extension for Dynamics 365 Business Central (SaaS)? The answer is yes, but the way you read the file is different. If you try to use the LoadData method, this is the result:

CSVBuffer_01.jpg

Here you’re in the cloud and you need to use Streams for accessing files. This is the code that works in a cloud Dynamics 365 Business Central environment:

CSVBuffer_02.jpg

The key here is to create an InStream object, reading the CSV file to the InStream object and then call the LoadDataFromStream method of the CSV Buffer table. Then you can parse the CSV content exactly like on the old C/AL code.

4 Comments

    1. Just use an skip like:

      var

      StartImport: boolean;

      begin

      repeat

      if StartImport = true then begin

        (your code for the import)

      end

      else

        StartImport = true;

      until (whatever).next = 0;

      So it will start with a false on “StartImport” and skip the first line because he is just going into the else and will set the StartImport on true. After this he will repeat the process with your import until the import is done.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.