How To Export To Text Files From Microsoft’s Business Central SAAS
Lead Dynamics Business Central Developer|Kerv Digital
Published 06/07/22 under:
Have a question?Get in touch
Before going down the development route, it’s always good to see what standard has to offer
Often during the lifecycle of a project there comes a time where you’ll need to export data from Microsoft Dynamics 365 Business Central in a particular format. Before going down the longer route of development though, it’s always a good idea to see what standard has to offer…
In the below example, there are “Open” and “Edit” in excel options on most pages which may offer the solution for you.
The reason I’ve chosen to share this is because I come across often the need to export payment journal lines. This will allow the customer to upload payment lines into their bank account to pay their vendors.
The below solution will cut out the process of them having to enter everything line by line into their banking software.
As you’d expect, the file the bank is expecting is quite strict in format.
Please note, with the introduction of data definitions this could also eliminate the need of any development, but I’ve seen many which are not possible using this method and the only option we have is to develop the solution.
Life has changed between NAV > BC and CAL code to AL code. The methods / code we used in the past may not work going forward for D365 Business Central SaaS.
Below is a method we can use to extract data from NAV to a text file, this method isn’t limited to just using text files.
Firstly, lets create a codeunit and within a function to handle our code.
To begin this process we need to add some variables to support our export
- One InStream
- One OutStream
- One as a reference to the standard “Temp Blob” codeunit.
- One to handle the file name but is not 100%, you can do this is many ways.
To enable us to write text to this we will need to create an OutStream using the Temp Blog variable.
Once created use the WriteText function within the OutStream variable.
This is a basic function below but could easily be extended to use two char variables to enable line feed / carriage return to add more lines.
Lastly to get the file to the user we would use the temp blob codeunit to create an InStream and DownloadFromStream to retrieve the file
To test this is working, I create a simple page extension to the company information page
The file is downloaded to the user: