Tagged Dynamics AX

X++ syntax highlighting with WP-GeSHi-Highlight

While I was setting up this blog, I installed WP-GeSHi-Highlight for X++ syntax high lighting. I wrote about it here. At the end of that post I wrote that I would edit the CSS of the plugin to make it look more like the X++ Editor.

I make a few small changes, this is what I have come up with so far:

  1. /// <summary>
  2. /// This job is used as an X++ sample (From Wikipedia's Microsoft Dynamics AX page)
  3. /// </summary>
  4. public static void xppTest3(Args _args)
  5. {
  6.     UserInfo userInfo;
  7.     ttsbegin;
  8.     select forupdate userInfo
  9.         where userInfo.id != 'Admin'
  10.            && userInfo.RecId == 1234567890;
  11.     while (userInfo)
  12.     {
  13.         userInfo.enable = NoYes::No;
  14.         userInfo.update();
  15.         next userInfo;
  16.     }
  17.     ttscommit;
  18. }

and this is the same code in the MorfX editor in 2012.
X++ code example

The original CSS produced this result:

I removed the dot from the line numbers and make the numbers turquoise. I also made the line height less. If you by chance use WordPress to write about X++ and have the WP-GeSHi-Highlight plugin, you are welcome to the changes I made to wp-geshi-highlight.css. The changes are small, but it just makes me feel a bit more at home with the way the code is displayed.

Workbook.ExportAsFixedFormat in Microsoft Dynamics AX

How to export a file to PDF in Dynamics AX

Microsoft has an extremely useful .NET method named ExportAsFixedFormat() for exporting office files to PDF. For example, you can invoke the ExportAsFixedFormat() method on an Excel Workbook object and a PDF file is then created in the folder of your choice.

Unfortunately the documentation on how to use this in function in Dynamics AX is a little lacking. In the .NET documentation most of the parameters are described as optional, while in Dynamics AX you have to pass all the parameters.

The last parameter is a pointer to the FixedFormatExt class. It is not necessary to pass an actual value, but the method does not accept null. Saveen Reddy describes in this post how to pass an empty value in C#. In Dynamics AX is even more tricky, since you do not have access to System.Reflection.Missing.Value. You need to create an object like this:

  1.     System.Type                  type           = System.Type::GetType("System.Reflection.Missing");
  2.     System.Reflection.FieldInfo  info           = type.GetField("Value");
  3.     System.Object                missingVariant = info.GetValue(null);

The final job is below, notice:

  1. All the parameters are passed, none are optional (line 32)
  2. The last parameter is a ‘null’ object that uses System.Reflection.Missing (lines 20-22)
  3. The method is in a try and is followed by a catch for a CLRError. This makes debugging much easier since it displays the .NET error which would otherwise not have been shown (lines 42-54 )
  4. I have used System.Type since I was worried AX variables might confuse .NET. As far as I know you could use normal AX str and int variables (lines 13-18)

  1. static void ExcelExportPDFAsFixedFormat(Args _args)
  2. {
  3.     #File
  4.     Microsoft.Office.Interop.Excel.Application  xlApp;
  5.     Microsoft.Office.Interop.Excel.Workbooks    xlWBS;
  6.     Microsoft.Office.Interop.Excel.Workbook     xlWB;
  8.     System.Exception                            exception;
  10.     Filename                                    filename =  @'C:\PDF\test3.pdf';
  11.     Filename                                    excelFileName = @'C:\PDF\TR1-000058.xlsx';
  13.     System.Int32                                toPage                  = 1;
  14.     System.Int32                                fromPage                = 1;
  15.     System.Boolean                              OpenAfterPublish        = false;
  16.     System.Boolean                              IncludeDocProperties    = true;
  17.     System.Boolean                              IgnorePrintAreas        = false;
  18.     System.String                               fileNameDotNet          = filename;
  20.     System.Type                                 type = System.Type::GetType("System.Reflection.Missing");
  21.     System.Reflection.FieldInfo                 info = type.GetField("Value");
  22.     System.Object                               missingVariant = info.GetValue(null);
  24.     new InteropPermission(InteropKind::ClrInterop).assert();
  26.     xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
  27.     xlWBs  = xlApp.get_Workbooks();
  28.     xlWB   = xlWBS.Add(excelFileName);
  30.     try
  31.     {
  32.         xlWB.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType::xlTypePDF,
  33.                                  fileNameDotNet,
  34.                                  Microsoft.Office.Interop.Excel.XlFixedFormatQuality::xlQualityStandard,
  35.                                  IncludeDocProperties,
  36.                                  IgnorePrintAreas,
  37.                                  fromPage,
  38.                                  toPage,
  39.                                  OpenAfterPublish,
  40.                                  missingVariant);
  41.     }
  42.     catch(Exception::CLRError)
  43.     {
  44.         exception = ClrInterOp::getLastException();
  45.         while (exception)
  46.         {
  47.             error(exception.get_Message());
  48.             exception = exception.get_InnerException();
  49.         }
  50.     }
  51.     catch
  52.     {
  53.         error("An unknown exception has occurred");
  54.     }
  55.     CodeAccessPermission::revertAssert();
  56. }

It is also possible to create PDF’s form Word Documents. I’ll post an example if I write a job to do that in the future.

UPDATE 26 December 2014:
A senior colleague wanted to achieve something similar and I offered him this job. He pointed out that an instance of Excel is opened every time the job is run, but never closed. You can see this clearly in the Task Manager.

If you want to use this job, experiment with adding xlWB.Close and/or xlApp.Quit, and keep an eye op the running applications in the Task Manager.

X++ syntax highlighting for WordPress

I’m still setting up the blog and was looking for a syntax highlighter plugin for X++. I tried a few highly rated syntax highlighters. Most does not have X++ support, so I tried using C++ and Java. All of the plugins formatted the code nicely and made the comments green, but of course did not make X++ keywords like select and while blue.

I consider X++ to be a niche, so I was surprised to find WP-GeSHi-Highlight that has support for X++. It’s highly customizable with arguments and CSS.

The plugin uses GeSHi highlight system, a .php library for formatting syntax on the web. I saw two other WordPress plugins based on GeSHi, but haven’t tried them yet:
Developer Formatter

It displays X++ code in WordPress like this:

Here’s the same code in the MorfX editor in 2012:
X++ code example

I’m going to try it and see how it goes. I’ll probably also make a few changes to the CSS to see if I can make it look more like the MorfX editor. I’ve noticed that it does not make integers red or comments italics, as you can see above.

UPDATE: I changed the CSS so that it looks a bit more like the MorfX editor. You can download the updated CSS at the end of this post.

Hello world!

I thought I would just leave the default WordPress first post title. For a blog of this nature it just felt too appropriate to spend any time on a more creative title.

Around six months ago I became a Microsoft Dynamics AX developer. Before I started working as an AX developer, I had never heard of Axapta, Dynamics Ax or any ERP software for that matter. Some of my friends where SAP developers, but I mostly pretended to know what that entailed. I was vastly unprepared for the world of programming that awaited me.

Form about the very first day I realized I wanted to write everything that I learn down. I was constantly getting stuck with simple tasks and had to disturb a senior developer to explain rudimentary things to me. I had a notepad, but I frequently did not have enough time to write everything on paper, especially if it involved a few lines of code. I knew I needed a blog.

It’s been six months since I started developing, so why start it now?

  • I am busy working on a data upgrade form Ax 4.0 to Ax 2012. It’s not what I expected at all. It’s a lot of trial and error and I would like to document our experiences.
  • Recently two developers started at my company. Like me, they have zero AX experience. While helping them, I realized I have actually come a long way since starting out. I learn new things every day, so the sooner I start writing them down the better.
  • I want to start studying for the Development certification and this will be a good platform to make and save study notes.
  • I had a fourth reason and as soon as I remember what it was, I’ll come back and edit this post.

Welcome to my blog where I save snippes of code and study notes of everything I learn regarding Axapta/ Microsoft Dynamics Ax and X++!