Tagged X++

Compile error: Microsoft.Office.Interop.Excel…

A few months ago I wrote a post on how to convert Excel documents to PDF with X++. Well, that piece of code made it into production! :) But I just notice it does not necessarily compile with Ax Build. :(

The code:
Excel Interop error

The error:
The xlApp declaration does not compile because Excel is not installed on the server and AX build can only be completed on the server. If you compile this same piece of code in the AOT on a server that has Excel installed, it compiles without a problem.

Excel Interop syntax error

My solution(s):

  • Install Excel on the server. I am not sure if this will allow AX Build to compile the code, but at least you can then open the AOT after AX build on the sever and compile like I described here.
  • Since I could not install Excel on the server, I did the following:
    1. Compile with AX Build on the server.
    2. If you have a compile error related to Microsoft.Office.Interop.Excel, start the server and open the AOT on a terminal server that has Excel installed.
    3. Compile the code with normal F7 compile.
    4. Compile the CIL on the terminal server.

Hope that helps! Let me know in the comments if you know of a better way to solve this.

How to add a range or data source to a form data source

Adding a second datasource with X++, or a range to a form datasource should be simple, yet today I spent ages trying strange things with multiple records. In the end it took me, two senior developers and a existing example in Dynamics AX to get to this simple solution. It might look silly, but I am definitely documenting this one for future reference!

I used to do the same thing by adding code in the form’s class declaration and the data source’s init and executeQuery methods. That is way too complex. Just add the code below to the data source’s init. Also, make sure you do this after the super() call, otherwise this query will not be initialized.

Add a range to a form
Suppose you have a form, with a PurchLine datasource added as a node in the AOT:

FormDatasource

If you only want to see purchase lines that are linked to open orders, then you need to add a range with PurchStatus equal to Backorder. You create this range in the data source’s init method:

  1. public void init()
  2. {
  3.     QueryBuildDataSource        qbdsPurchLine;
  4.  
  5.     super();
  6.  
  7.     qbdsPurchLine = this.query().dataSourceTable(tableNum(PurchLine));
  8.     qbdsPurchLine.addRange(fieldnum(PurchLine, PurchStatus)).value(queryvalue(PurchStatus::Backorder));
  9. }

Add an additional datasource to the form
Now suppose you want to filter the PurchLine based on some criteria on the PurchTable. For example, you only want to see lines from derived intercompany orders. (This is just an example, PurchLine actually has it’s own intercompany origin field.) You could add an additional PurchTable datasource to the form, and join the datasources in the form properties. Every easier is adding the following to the init of the PurchLine datasource:

  1. public void init()
  2. {
  3.     QueryBuildDataSource        qbdsPurchLine;
  4.     QueryBuildDataSource        qbdsPurchTable;
  5.  
  6.     super();
  7.  
  8.     qbdsPurchLine = this.query().dataSourceTable(tableNum(PurchLine));
  9.     qbdsPurchLine.addRange(fieldnum(PurchLine, PurchStatus)).value(queryvalue(PurchStatus::Backorder));
  10.  
  11.     qbdsPurchTable = qbdsPurchLine.addDataSource(tableNum(PurchTable));
  12.     qbdsPurchTable.addLink(fieldNum(PurchTable, PurchId), fieldNum(PurchLine, PurchId));
  13.     qbdsPurchTable.addRange(fieldnum(PurchTable, InterCompanyOrigin)).value(queryValue(InterCompanyOrigin::Derived));
  14. }

You can declare as many QueryBuildDataSource variables as you need, and add them like the PurchTable above.

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:
CSS

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.

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
WP-SynHighlight

It displays X++ code in WordPress like this:
CSS

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.