Writing UNIX style text files

I’ve written many mods for AX that required output of data to a text file of some form (the most common being CSV), but recently I had to ensure that the file being written was compliant with a UNIX reader.

The default output for files created from within AX are UTF-16LE with the standard Windows CR+LF new-line characters. A UNIX system that expects ANSI formatting with just a single LF character as the new-line sequence will not be able to read these files due to additional characters prepended to the beginning of the file for the code page designator and the additional carriage return character at the end of each line.

The TextIo object in AX provides us with the necessary settings to allow this specific type of output with very little additional coding.

static void UNIX_TextFile(Args _args)
    str outputPath = @"C:\Temp\ANSI.txt";
    TextIo outputFile = new TextIo(outputPath,"W", 0);//ANSI code page

    outputFile.outRecordDelimiter(num2char(10)); //UNIX line-feed character
    outputFile = null;

In the above example, using 0 as the code page parameter when creating the TextIo object specifies that the output file should use ANSI formatting (additional code page values can be found on this MSDN page).

In order to use the UNIX style new-line, we call the outRecordDelimiter method with the numeric value of the UNIX line-feed character 10. This causes all “records” written to the file to be followed with a single line-feed character instead of the standard Windows carriage-return/line-feed sequence.

There are many Blog entries around the web that go into much greater detail of how the File IO classes available in AX actually work, but I was unable to find any that specifically outlined a solution for writing UNIX styled text files from within AX so I decided to provide one of my own. If you know of any others feel free to provide links.


Posted in AX 2012 | Tagged , , , , | Comments Off

AX Model Manager

With previous versions of AX utilizing the graphical interface for XPOs to deploy code, working with the Models & Models stores in AX 2012 using the command line tools may at times seem a bit tedious. With this in mind I began investigating the various functionality made available by the AxUtil.exe application as well as the PowerShell functions.

Utilizing the same code libraries as the command line tools I was able to create a graphical interface that, at this point, contains most of the same basic functionality.


The graphical interface currently implements the following functionality:

  • Set target SQL Server
  • Select target AX Model Store database – model store databases are populated for the given server.
  • Select target Model – populated models in the selected store are available for selection in a drop down box.
  • Model Import – provides drop down for conflict resolution options
  • Model Export – includes the ability to provide a file for strong name signing
  • Model Deletion – removes the selected model from the target model store (database operation only)
  • Model Manifest view – displays the properties of the currently selected model from the model store, or a model file opened for importing.
  • Model Store Export – exports the currently selected model store to the specified file
  • Model Store Import – provides drop down for ID conflict resolution options as well as target schema name

The feedback that you normally see on the screen is redirected to the output tab with the most recent message displayed on the status bar.

If there is any level of interest among the AX community, I will look into distribution options.

Posted in AX 2012, Microsoft Dynamics AX | Tagged , , , , , , , | 4 Comments

Objects not in Version Control (Pre 2012)

Due to the level of interest that I received on my last post, I’ve decided to go ahead and release this version of the “Objects not in Version Control” script. I’ve cleaned up the code and added some improvements that have drastically improved performance, but it still does not compare to the efficiency of the Model Store in AX 2012.

Please note that the following script is nowhere near as quick as the one posted for AX 2012 due to the performance issues associated with the UtilElement views in older versions of AX.

static void ObjectsNotInVCS(Args _args)
    SysVersionControlSystem vcsSys;
    SysVersionControllable  controlable;

    Set             parsed      = new Set(Types::String);
    UtilEntryLevel  curLayer    = currentAOLayer();

    UtilElements    child, parent;
    xRefPath        nodePath;
    TreeNode            pNode;
    //If version control is not enabled, no need to continue
    vcsSys = versionControl.parmSysVersionControlSystem();
        info("Version control not enabled");

    //Iterate through all item in the current layer
    // You can add additional filtering here to reduce
    // the number of objects parsed.
    while select child
        where child.utilLevel == curLayer
        //Get the parent node of the current object
        parent = xUtilElements::parentElement(child);

        pNode  = SysTreeNode::findNodeInLayer(
                    parent.recordType, parent.name, 0, curLayer);

        controlable = SysTreeNode::newTreeNode(pNode);


If you have any tips or tricks for improving the performance of this script, please feel free to add a comment.

Posted in AX 2009, AX 4.0, Microsoft Dynamics AX | Tagged , , , , , , | Comments Off

Objects not in Version Control (AX 2012)

If you’ve ever worked in a development environment that utilizes version control I’m sure that you have fell victim to the “missing objects” error during the build phase. This usually happens because sometimes you create objects in the AOT and forget to add them to version control. And at some point you check in other objects that contain references to those missing objects and the build server fails.

In previous versions of AX I developed scripts to scan AOT objects and determine which ones had not been added to version control. These scripts could be very slow executing and usually ended up being used infrequently. Working in AX 2012 I have discovered a quick means of parsing new and modified AOT objects that utilizes the model stores and I applied it to my “Objects not in VCS” script. This new methodology is very quick at determining which items in the system have not been included in the version control system.

By default the following job only scans the current layer and the active model (version control works by layer and model). The model restriction can be omitted to open up scanning of all objects in the layer, but it would defeat the purpose of the script if you were to remove the layer limitation.

Continue reading

Posted in AX 2012, Microsoft Dynamics AX | Tagged , , , , , | Comments Off

Language Specific Number Formatting

I recently encountered a situation in AX where I needed to convert a number into a string that was specific to the current user’s language setting.

AX provides a method called num2str for converting a real number into a formatted string. However, this function requires the user to specify in code which separators are to be used in the output and is not specific to the language of the current user. In order properly format the output based on the user’s language preference, you would need to parse the culture information for the language and then pass those values in to the AX method. You would also need to add any currency identifier if the output was to be used as a currency.

To resolve this problem I created a static method on the Global class that utilizes the culture specific number formatting available in the .Net framework. This method allows you to specify the output format as well as the desired precision and is outlined below.

Continue reading

Posted in AX 2012 | Tagged , , , , , , , , , | Comments Off

Set Focus to Main Content in EP

Recently while working with a custom control in EP for AX 2012 I came across an issue when trying to set focus to a specific Textbox. I implemented all of the standard focus functions available in code, but none of them would retain focus when the control was running in the AX EP environment. I also tried a multitude of suggested fixes found on various Blogs with no luck. So I started my on investigation and the following is my findings.

I noticed that the target control would temporarily receive focus and then immediately lose it to a hidden system menu that exists at the top of the page. I dug deeper into this behavior and it led me to the DynamicsRestoreFocus java script that is available in the static file EPRestoreFocus. This script contains methods that are used to call focus to the various parts of an EP page.

Continue reading

Posted in AX 2012, Enterprise Portal, Microsoft Dynamics AX | Tagged , , , , , | Comments Off

Automatic Class Pack/Unpack Part 2

In a previous post I demonstrated a way to pack & unpack a class in a manner that would always be version independent without the use of macros. In AX 2012 Microsoft added attribute support for classes and methods. When I discovered this new functionality, I knew it would be ideal for improving my old code. So I created a class named PackValueAttribute which extends the new SysAttribute class. This allows the programmer to simply tag a method with the new attribute and it’s value will automatically be included in any pack/unpack method calls.

Continue reading

Posted in AX 2012, Microsoft Dynamics AX | Tagged , , , , , | 2 Comments

Posting a Transfer Order using X++

While working with unit testing in AX, I came across the need to generate a transfer order and post it to generate the associated tables. I have used the FormLetter classes for posting Sales Orders & Purchase Orders in the past so I started looking for a similar process for Transfer Orders. I started by examining the code that is used in AX for the same purpose. This search led me to the following information.

Continue reading

Posted in AX 2012, Microsoft Dynamics AX | Tagged , , , , | 6 Comments

Last Weekday of Month

This method will calculate the date of the last specified weekday in the month of the given date.

static date lastWeekdayOfMonth(date _date, WeekDays _weekday){
    int     _endWeekDay, _diff;
    date    _endDate = endmth(_date);
    _endWeekDay = dayofwk(_endDate);
    _diff       = _endWeekDay - _weekday;
    _date       = ((_endDate) - _diff);

    if (_diff < 0)
        _date = ((_date) - 7);

    return _date;

Continue reading

Posted in AX 2009, AX 2012, AX 4.0, Microsoft Dynamics AX | Tagged , , | Comments Off

Calculating a File Hash

Some time ago a colleague presented me the task of computing a SHA256 hash for an output file. This hash would be stored in a table in a hexadecimal string format, along with the file info, to ensure that the file wasn’t modified by any external sources.

To complete this task I generated the following code that uses CLRInterop to access the .Net System.Security.Cryptography namespace to compute the hash. The returned Byte Array is then converted into an X++ List object by utilizing an example I presented in another post named CLR Array to AX List. This List is then converted into a hexadecimal string.

Continue reading

Posted in AX 4.0, Microsoft Dynamics AX | Tagged , , , , , | Comments Off