Create and save csv or text file in D365 Finance and Operations

Thursday, December 31, 2020

In this example, we are going to create a CSV or Text File in Dynamics 365 Finance and Operations. We're going to fill the file with customer records from the CustTable table and download the file locally using your browser. Lets start by adding a new runnable class to our Visual Studio Project. Right Click project name and select add.Step 1 image

Lets add a new class to our Visual Studio Project. Right Click project name. Right Click our project in Visual Studio, if you don't know how to create a project refer back to our earlier tutorials 

Step 2 image

In the pop up window or menu items which is displayed , Click the Add menu item.Step 3 image

Click New Item... In the pop up window which is displayed I am going to select code in the left hand window. This will show me all the code options I can add.Step 4 image

Select runnable class in the code pane options and give the class a name. I am going to call the class RunnableClassCreateCSV Step 5 image

Click Name at the bottom of the dialog or window to give the class the name you would like.Step 6 image

In the Name insert the name of the class RunnableClassCreateCSV Step 7 image

In the code pane of the class I'll paste some code i prepared earlierStep 8 image

The code we will insert in in the screen shot below. The full code is at the bottom of the article as well  We start by creating a text stream object. the next line then writes to the object and populates it with the header of our file using the write method.We then open the CustTable making sure we are in the USRT company , this is an optional steps and just swithces the company to the one sepecified. We then initialise a while look and populate all the customer records into the stream method using the same write method we used previously. 

Now we have a stream of content we need to read it before it can be send to a file. The stream reader does this. When the stream reader is initialised it is with the text stream object. Reading the  Content to the end converts this to our XML object.

The sendstringasfiletouser method of the file object does the magic and sends the file to the browser.Step 9 image

Click highlight in the solution explorer and set the new class as our startup object, this will run this code when we start to run the project

Step 10 image

11. Next let's run the code and see what the comes out of the code. You can debug or start without debugging depending on how you want to review the code running

Step 11 image

12. Once the code is running. A browser window pops up with your code running in it. In this example we can see that a file has been created called MyCSVFile.csv, save this file 

Step 12 image

13. Click View downloads to see the file the file was was created. There should be an excel or csv file in the download directory containing all the information which we have exported.

Step 13 image

14. Click Open to view the file 

Step 14 image

15. And that's all there is to it. The file was created, it has four columns and it contains all the records for the cust table for this company. That's it you're done. 

Step 15 image

  class RunnableClassCreateCSV
  {
  /// <summary>
  /// Runs the class with the specified arguments.
  /// </summary>
  /// <param name = "_args">The specified arguments.</param>
  public static void main(Args _args)
  {
  System.IO.Stream stream;
  TextStreamIo io;
  System.IO.StreamReader reader;
  XML fileContent;
   
  io = TextStreamIo::constructForWrite();
  io.write("AccountNum,CustomerName,Currency,Customer Group");
   
  CustTable custTable;
  //Assume that you are running in company 'DAT'.
  changeCompany('USRT') //Default company is now 'USRT'
  {
  custTable = null;
  while select custTable
  {
  io.write(strFmt("%1,%2,%3,%4",custTable.AccountNum,custTable.name(),custTable.Currency,custTable.CustGroup));
  }
  }
   
  stream = io.getStream();
  stream.Position = 0;
  reader = new System.IO.StreamReader(stream);
  filecontent = reader.ReadToEnd();
   
  //save file on local folder via the browser
  File::SendStringAsFileToUser(fileContent, 'MyCSVFile.csv');
  }
  }