Friday, February 12, 2021

Creating a simple text file using AL in Business Central - Sample Code

 By Steve Endow

On January 23, 2021, I recorded a video showing how to create a simple text file using AL in Business Central.


Here is the video:

https://www.youtube.com/watch?v=_GUzlFKJFQk


Below is the sample code from that video.

First, a few source pages that I used to create my demo.

1. A blog post showing the basics of creating a text file using AL:

https://www.cloudthing.com/blogs/export-to-text-files-from-business-central

2. A Microsoft Docs page discussing working with text file line endings in AL:

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-write-read-methods-line-break-behavior

3. A Microsoft Docs page discussing the Duration data type:

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/duration/duration-data-type


Coincidentally, Yun Zhu also recently wrote a comprehensive blog post discussing usage of the Duration data type:

https://yzhums.com/9273/


First, I created a page called ExportExample.PageExt.al, which contains this AL code to create Action buttons we'll use to create the text files.

 pageextension 50101 "ExportExample" extends "Company Information"  
 {  
   layout  
   {  
   }  
   actions  
   {  
     addlast(Processing)  
     {  
       Action("Export Text 1")  
       {  
         ApplicationArea = All;  
         Promoted = true;  
         PromotedIsBig = true;  
         PromotedCategory = Category4;  
         Image = ExportElectronicDocument;  
         trigger OnAction()  
         var  
           ExportExample: Codeunit "ExportExample";  
         begin  
           ExportExample.CreateTextFile1();  
         end;  
       }  
       Action("Export Text 2")  
       {  
         ApplicationArea = All;  
         Promoted = true;  
         PromotedIsBig = true;  
         PromotedCategory = Category4;  
         Image = ExportElectronicDocument;  
         trigger OnAction()  
         var  
           ExportExample: Codeunit "ExportExample";  
         begin  
           ExportExample.CreateTextFile2();  
         end;  
       }  
     }  
   }  
 }  

I then created a Codeunit called ExportExample.Codeunit.al to store the AL code that would actually create the sample text files.

It includes some other items I was playing with to learn, like recording elapsed time and displaying notifications on the page.

 codeunit 50100 "ExportExample"  
 {  
   //Create a simple BC AL text file  
   //https://www.cloudthing.com/blogs/export-to-text-files-from-business-central  
   //Line Endings:  
   //https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-write-read-methods-line-break-behavior  
   //Duration:   
   //https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/duration/duration-data-type  
   trigger OnRun()  
   begin  
   end;  
   procedure CreateTextFile1()  
   var  
     InStr: InStream;  
     OutStr: OutStream;  
     tmpBlob: Codeunit "Temp Blob";  
     FileName: Text;  
     StartTime: Time;  
     EndTime: Time;  
     ElapsedTime: Integer;  
     //HowLong: Duration;  
     CRLF: Text[2];  
     Counter: Integer;  
   begin  
     StartTime := System.Time;  
     CRLF[1] := 13;  
     CRLF[2] := 10;  
     FileName := 'TestFile.txt';  
     tmpBlob.CreateOutStream(OutStr, TextEncoding::Windows);  
     OutStr.WriteText('Start time: ' + Format(StartTime) + CRLF);  
     //OutStr.WriteText();  
     for Counter := 1 to 5000000 do begin  
       OutStr.WriteText(Format(Counter) + ': This is a line in the text file' + CRLF);  
       //OutStr.WriteText();  
     end;  
     EndTime := System.Time;  
     OutStr.WriteText('End time: ' + Format(StartTime) + CRLF);  
     // OutStr.WriteText();  
     ElapsedTime := EndTime - StartTime;  
     OutStr.WriteText('Elapsed time: ' + Format(ElapsedTime));  
     tmpBlob.CreateInStream(InStr, TextEncoding::Windows);  
     DownloadFromStream(InStr, '', '', '', FileName);  
   end;  
   procedure CreateTextFile2()  
   var  
     InStr: InStream;  
     OutStr: OutStream;  
     tmpBlob: Codeunit "Temp Blob";  
     FileName: Text;  
     StartTime: Time;  
     EndTime: Time;  
     ElapsedTime: Integer;  
     HowLong: Duration;  
     CRLF: Text[2];  
     Counter: Integer;  
     MyNotification: Notification;  
     ElapsedTimeText: Text;  
     HowLongText: Text;  
   begin  
     StartTime := System.Time;  
     CRLF[1] := 13;  
     CRLF[2] := 10;  
     FileName := 'TestFile.txt';  
     tmpBlob.CreateOutStream(OutStr, TextEncoding::Windows);  
     OutStr.WriteText('Start time: ' + Format(StartTime) + CRLF);  
     //OutStr.WriteText();  
     for Counter := 1 to 1000 do begin  
       OutStr.WriteText(Format(Counter) + ': This is a line in the text file' + CRLF);  
       //OutStr.WriteText();  
     end;  
     EndTime := System.Time;  
     OutStr.WriteText('End time: ' + Format(StartTime) + CRLF);  
     // OutStr.WriteText();  
     ElapsedTime := EndTime - StartTime;  
     HowLong := EndTime - StartTime;  
     ElapsedTimeText := 'Elapsed time: ' + Format(ElapsedTime) + 'ms';  
     HowLongText := 'Duration: ' + Format(HowLong);  
     OutStr.WriteText(ElapsedTimeText);  
     OutStr.WriteText(HowLongText);  
     tmpBlob.CreateInStream(InStr, TextEncoding::Windows);  
     DownloadFromStream(InStr, '', 'D:\Temp', '', FileName);  
     MyNotification.Message := ElapsedTimeText + ' . . . . ' + HowLongText;  
     MyNotification.Scope := NotificationScope::LocalScope;  
     MyNotification.AddAction('Thank you!', Codeunit::ExportExample, 'ThankYou');  
     MyNotification.AddAction('Thank you very much!', Codeunit::ExportExample, 'ThankYouVeryMuch');  
     MyNotification.Send();  
   end;  
   procedure ThankYou(MyNotification: Notification)  
   begin  
   end;  
   procedure ThankYouVeryMuch(MyNotification: Notification)  
   begin  
   end;  
 }  


Steve Endow is a Microsoft MVP in Los Angeles.  He works with Dynamics 365 Business Central, Microsoft Power Automate, Power Apps, Azure, and .NET.

You can also find him on Twitter and YouTube

3 comments:

  1. where is tmpBlob: Codeunit "Temp Blob"; ? i dont't found it

    ReplyDelete
  2. Hi, the Codeunit "Temp Blob" should be accessible if you have Downloaded Symbols in VS Code. (Unless something has changed in the last year)

    Are you able to successfully download symbols?

    ReplyDelete

All comments must be reviewed and approved before being published. Your comment will not appear immediately.

How many digits can a Business Central Amount field actually support?

 by Steve Endow (If anyone has a technical explanation for the discrepancy between the Docs and the BC behavior, let me know!) On Sunday nig...