Book Image

Microsoft Dynamics AX 2009 Development Cookbook

By : Mindaugas Pocius
Book Image

Microsoft Dynamics AX 2009 Development Cookbook

By: Mindaugas Pocius

Overview of this book

Microsoft Dynamics AX provides a comprehensive Enterprise Resource Planning (ERP) solution for mid-size and larger organizations. Dynamics AX implementations are used worldwide by thousands of customers. With the new version - Dynamics AX 2009 - the system is due to expand even more rapidly. Every new implementation requires some level of customization, and all organizations want this to be done to the highest standards using proven approaches. This step-by-step guide will help you manage your company's ERP information and operations efficiently, and solve your business process problems in an effective and quick way. This book focuses on commonly used custom modifications in major Dynamics AX modules. This book contains commonly used bits of code which were real-world tested in at least one successful Dynamics AX implementation. Many of the recipes were deployed in many implementations and even across several versions of Dynamics AX. The examples in this book cover various areas of Dynamics AX to help developers not only learn about programming, but also about the functional side of Dynamics AX. The book should help developers who already know the basics of Dynamics AX programming to step up to the next level. The recipes allow you to look at the development from the perspective of business processes. You will learn to enhance your user interface using various Dynamics AX UI elements. Managing your data and functions will become easier. You will also get numerous development tips and tricks from a Dynamics AX development expert.
Table of Contents (12 chapters)
Microsoft Dynamics AX 2009 Development Cookbook
Credits
About the Author
About the Reviewers
Preface
Index

Reading a comma-separated value file


Besides data import/export, CSV files could be used for integration between systems. It is probably the most simple integration approach. I've seen and worked on a number of projects where an external application, for example a specialized billing system, generates CSV files every day. Dynamics AX runs a periodic batch job, which reads the generated files every night and imports the data, for instance, sales orders. Although this is not a real-time integration, in most cases it does the job and does not require any additional components like Dynamics AX Application Integration Framework or something similar.

Another well known example is when external companies are hired to manage the payroll. On a periodic basis, they send CSV files to the finance department, which are then loaded into the General journal in Dynamics AX and processed as usual.

In this recipe, we will learn how to read CSV files from code. As an example, we will process the file created in a pervious recipe.

How to do it...

  1. In AOT, create a new class named ReadCommaFile with the following code. Replace <documents> with your own path (use double backslashes for folder separation, i.e. \\):

    class ReadCommaFile 
     {
     }
     
     public static client void main(Args _args)
     {
         CommaIo   file;
         container line;
         #define.filename('<documents>\\accounts.csv')
         #File
         ;
     
         file = new CommaIo(#filename, #io_read);
     
         if (!file || file.status() != IO_Status::Ok)
         {
             throw error("File cannot be opened.");
         }
         
         while (file.status() == IO_Status::Ok)
         {
             line = file.read();
             if (line != connull())
             {
                 info(con2str(line, ' - '));
             }
         }
     }
  2. Run the class to view the file's content:

.

How it works...

As in the previous recipe, we first create a new file object using CommaIo. This time we use #io_read as the mode to make sure that the existing file is opened for reading only. We also perform the same validations to make sure that the file object is correctly created, otherwise we show an error message.

Next and finally, we read the file line by line until we reach the end of the file. Here we use the while loop until the file status changes from IO_Status::OK to IO_Status::FileTooShort, which means no more lines exist in the file. Inside the loop, we call read() on the file object, which returns the current line as a container and moves the internal file cursor to the next line. File data is then simply outputted to the screen using the standard global info() function in conjunction with con2str(), which converts a container to a string for displaying.

The last element of code where data is outputted normally should be replaced by proper code that processes the incoming data.

There's more...

File reading, like file writing, could also be executed on a server to improve performance. The modifier client has to be changed to server, and code with the FileIoPermission class has to be added to fulfill the code access security requirements. The modified class should look like the following:

class ReadCommaFileServer 
 {
 }
 
 public static server void main(Args _args)
 {
     CommaIo          file;
     container        line;
     FileIoPermission perm;
     #define.filename('<documents>\\accounts.csv')
     #File
     ;
 
     perm = new FileIoPermission(#filename, #io_read);
     perm.assert();
 
     file = new CommaIo(#Filename, #io_read);
 
     if (!file || file.status() != IO_Status::Ok)
     {
         throw error("File cannot be opened.");
     }
 
     while (file.status() == IO_Status::Ok)
     {
         line = file.read();
         if (line != connull())
         {
             info(con2str(line, ' - '));
         }
     }
 
     CodeAccessPermission::revertAssert();
 }