Import various file types with Interfaces

by Apr 15, 2021AL Language

Home 9 Development 9 AL Language 9 Import various file types with Interfaces

A few weeks ago, I described a solution for importing an Excel file to the Business Central using Excel Buffer in AL Language (see here). In today’s article, we will look at how to build a more complex solution that can be used for file imports without the needs to identify the imported file type.

The example is available on my GitHub (here). In the next parts, we will look in details at some of the parts from this repository.

Enum & Interface

First of all, we need to create an interface that describes which procedures must be implemented by each file format implementation (if you want to know more about AL Language enums & interfaces, check my previous articles about Enum Data Type or the series of Interfaces in AL (part 1), (part 2) and (part 3)).

For our example, we need only two procedures. You will probably need some more procedures to make a (super) universal solution in real extension, but two methods are enough for our example.

So every implementation that uses this interface has to implement ImportFile procedure (that accept Code[20] and returns temporary CSV Buffer) and FieldSeparatorSupported procedure (returns boolean).

 /// <summary>
 /// Interface TKA IImport File Format.
 /// </summary>
 interface "TKA IImport File Format"
 {
     /// <summary>
     /// Import lines from the select file. The file is selected by user. Based on implementation, some other selections may be neccessary (specific sheet/list/range, ...).
     /// </summary>
     /// <param name="ImportConfigurationCode">Code[20], definition of used import configuration</param>
     /// <returns>Return value of type Record "CSV Buffer" temporary, contains all lines from imported file.</returns>
     procedure ImportFile(ImportConfigurationCode: Code[20]) TempCSVBuffer: Record "CSV Buffer" temporary;

     /// <summary>
     /// Specifies whether field separator is supported for the format
     /// </summary>
     /// <returns>Return value of type Boolean.</returns>
     procedure FieldSeparatorSupported(): Boolean;
 }

We will also create an enum that defines our supported file types. For now, we will add support for Excel (xlsx) and CSV files. The important thing is to define enum using the keyword “implements” followed by the name of our interface. This construction specifies that all enum values have to implement this interface (it does not matter whether the value is included in the Enum definition or is added through EnumExtension).

 /// <summary>
 /// Enum TKA Import File Format (ID 50000) implements Interface TKA IImport File Format.
 /// </summary>
 enum 50000 "TKA Import File Format" implements "TKA IImport File Format"
 {
     Extensible = true;

     value(5; "Excel file (.xlsx)")
     {
         Caption = 'Excel file (.xlsx)';
         Implementation = "TKA IImport File Format" = "TKA Import File Format XLSX";
     }
     value(10; "CSV file (.csv)")
     {
         Caption = 'CSV file (.csv)';
         Implementation = "TKA IImport File Format" = "TKA Import File Format CSV";
     }
 }

Interface Implementations

Interfaces can be implemented using Codeunits with the “implements” keyword (similarly to enums).

CSV Files (Code on GitHub)

There is nothing special about the implementation of CSV file imports. The import is done using “CSV Buffer” functionality (that manage everything internally) and, as the ImportFile procedure must return CSV Buffer, we just can return the loaded records.

The procedure FieldSeparatorSupported returns true as CSV files needs separator character to split the values.

Excel Files (Code on GitHub)

The implementation of Excel files is very similar. It is done using Excel buffer (as was discussed in this article). The only difference from CSV file implementation is that we need to change the structure of loaded values from “Excel Buffer” to “CSV Buffer”.

FieldSeparatorSupported returns false as Excel does not use separator characters.

Finally, let’s import file

Once we have defined and implemented all supported file types, we can use our functionality just by calling the ImportFile procedure through an interface defined with the enum value.

For our example, I created a table that defined file configuration (file type and field separator, if necessary), available on GitHub. Using values in this table, we know which file type we are importing and which field separator we are using.

 /// <summary>
 /// Import lines using import configuration form current Record (Rec)
 /// </summary>
 /// <returns>Return value of type Record "CSV Buffer" temporary, contains all lines from imported file.</returns>
 procedure ImportFile(): Record "CSV Buffer" temporary
 var
     IImportFileFormat: Interface "TKA IImport File Format";
 begin
     IImportFileFormat := "TKA Import File Format";
     exit(IImportFileFormat.ImportFile(Rec."TKA Code"));
 end;

Recent Articles from the category

Connect to Azure Function in BC 2022 wave 2 (v21)

Connect to Azure Function in BC 2022 wave 2 (v21)

The new version of the Microsoft Dynamics 365 Business Central brought a new system module "Azure Functions" that makes integration with Azure Functions much easier and straightforward. The typical scenario, why to use Azure Functions together with Business Central,...

read more
Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use? This is the second part of my new article series about Collectible Errors. Let's check out the first part here: Collectible Errors?! | Microsoft Dynamics 365 - Ing. Tomáš Kapitán (kepty.cz) or you might be interested in my...

read more
Collectible Errors?!

Collectible Errors?!

Collectible Errors?! It has been already almost a year since ErrorInfo datatype & CollectibleErrors were introduced (I already have an article about basic structure: ErrorInfo data type & Collectible Errors). This article was released for the first time in...

read more
Substituting standard reports

Substituting standard reports

Report objects cannot be extended in versions previous to the Business Central 2021 release wave 1 version when ReportExtensions object was introduced. Since then, many of changes we need to do in reports can be done without creating a new copy of object. On the other...

read more
Isolated events

Isolated events

With Business Central 2022 wave 1, a new setting for event publishers was introduced. Until this version, any error in any event subscriber caused interruption to the current running process and stopped the whole activity. In some cases (such as log-in), this is...

read more
Custom Filter Tokens

Custom Filter Tokens

As a user of the Business Central you have some constants you can use to filter or insert values. These constants contain useful values for data manipulation such as t / today for date field - return current dateq / quarter for date field filters - return range of the...

read more
1D & 2D Barcodes in Business Central 19.1

1D & 2D Barcodes in Business Central 19.1

Business Central (cloud-only!) includes one-dimensional (1D) barcode fonts since Microsoft Dynamics 365 Business Central 2020 wave 2 (17.0) was released in October 2020. It is a great improvement to how developers can handle requirements from their clients to print...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here