Posts RSS Comments RSS 24 Posts and 45 Comments till now

Table browser with field groups

The table browser is a great tool but it can be hard to find the fields you’re looking for in the grid. Standard Ax either shows all fields or the fields in the AutoReport field group.

Sometimes I like to use another field group so I added that option to the table browser. Now I can use any field group defined on the table. Filtering out the irrelevant fields makes browsing the data a lot easier.

Table browser screenshot

It wasn’t too hard to implement. Standard Ax code can already handle any field group but there is no way to choose one. On the form SysTableBrowser I replaced the radio button with a combobox and then modified the class SysTableBrowser to use that instead of the radio button.

XPO for Dynamics Ax 4.0

Languages in 3-tier

Today I noticed several labels were missing in a customer’s application. After some investigation it turned out I had accidently logged in using a language they don’t have a license for.

I dug a little deeper and discovered that when using a 3-tier configuration I could choose any language. The AOS can be configured with any language as well. When switching to 2-tier Axapta displayed the expected error about unlicensed languages.

This was a 3.0 SP1 setup, perhaps it’s fixed in later versions.

SafeMap

Axapta’s Map data structure is something I use quite often. Maps, Set, List, Array and Struct may not be very well known data types but I prefer them over the often abused container.

Unfortunately Maps throw errors when you call lookup() with a key that doesn’t exist. Throwing an exception is a valid way of handling the situation but as I showed earlier, this can be a problem. The remedy is simple, check the key first with the exists() method. This however places a burden on the user of the map. Before you know it your code is littered with checks and that simple interface with a 3rd party system gets tedious to write.


    Map map = new Map(Types::String, Types::String);
    //...
    palletId = map.exists('Pallet')  ? map.lookup('Pallet')  : '';
    serialId = map.exists('Serial')  ? map.lookup('Serial')  : '';
    itemId   = map.exists('Product') ? map.lookup('Product') : '';
    // and so on ...

That’s not so easy to read and maintain. I finally got fed up with the duplication and wrote a simple Map subclass to do this checking for me: SafeMap. It’s safe because it doesn’t throw an exception. I guess you could consider that behaviour unsafe as well.

But what if the value doesn’t exist? Surely, lookup()can’t return a value. Yes it can. It returns the default null value for the map value type. Code that doesn’t care about the failed lookup, probably wants the null value anyway. And just in case the user of SafeMap needs to know if the lookup succeeded, there is a lookupOk() method. It simply returns a boolean that indicates if the last lookup succeeded or not.

The above code now becomes:


    Map map = new SafeMap(Types::String, Types::String);
    //...
    palletId = map.lookup('Pallet');
    serialId = map.lookup('Serial');
    itemId   = map.lookup('Product');

Much clearer, don’t you think?

After discovering the testing framework in Dynamics Ax 4.0 I decided to upgrade (read: export from 3.0 and import in 4.0 :) ) and at the same time throw in a very simple test class as an example. Check out David Pokluda’s SysTest quick start for more information about the framework.

SafeMap.xpo (Dynamics Ax 4.0)

Axapta error handling and database transactions

Exception handling in Axapta is a bit weird. Unlike C# or Java, exceptions aren’t full classes. In Axapta exceptions are defined in the enum Exception and that’s it. You can’t create your own exceptions and you can’t add data to the exception. If you want the error message you have to get it from the infolog. It’s limited but it usually doesn’t get in the way. Unless you add transactions to the mix.

The catch (no pun intended) is that the throw statement in Axapta also does an implicit ttsAbort if you’re in a transaction. And that’s where the confusion starts and computers get yelled at.

Suppose you’re doing some updates to several tables and need to stop processing when you detect an error. The simplified code would be something like this:


static void TryCatchOutsideTTS(Args _args)
{
    ;

    try
    {
        ttsBegin;

        // ...
        throw error('Catch me if you can');
        // ...

        ttsCommit;
    }
    catch (Exception::Error)
    {
        info('Gotcha');
    }

    info('EOF');
}

When you run this everything works as you’d expect:
exception1.PNG

So far so good. Now what about catching exceptions inside a transaction? A possible scenario is a loop to update records, logging errors and continuing with the next record when an error is encountered. The code boils down to:


static void TryCatchInTTS(Args _args)
{
    ;

    ttsBegin;

    //while select forUpdate ...
    try
    {
        // ...
        throw error('Catch me if you can');
        // ...
    }
    catch (Exception::Error)
    {
        info('Gotcha');
    }

    ttsCommit;

    info('EOF');
}

And this is what you get:

That’s strange. The catch block was not executed at all. Even worse, the part after the try/catch is ignored as well and the method ends immediately.

If you’re using a try/catch construct, you probably need to clean up whatever you’re doing if things go wrong. This shows there is no guarantee the catch block will be executed.

What if we add another try/catch?


static void DoubleTryCatch(Args _args)
{
    ;

    try
    {
        ttsBegin;

        //while select forUpdate ...
        try
        {
            // ...
            throw error('Catch me if you can');
            // ...
        }
        catch (Exception::Error)
        {
            info('Gotcha');
        }

        ttsCommit;

        info('What about me?');
    }
    catch (Exception::Error)
    {
        info('None shall pass');
    }

    info('EOF');
}

Which yields:
exception3.PNG

This behaviour surprised me at first but then I realized it’s the same as throwing a new exception inside a catch block. The ttsAbort makes it impossible to execute the rest of the transaction safely, even if part of it is outside the try/catch block. So the only option is to fall back to a higher catch block.

Usually this doesn’t matter. In some cases this can get in the way. Like when you’re using resources (open files, connections, …) you really should release when you’re done. Using resources in a transaction isn’t best practice but you could end up in that situation without realizing it. Whenever you reuse code, be it a standard API or a third party module, it could do things you’re not fully aware of.

There’s no simple solution to the problem. Just be careful and test thoroughly to make sure situations like this can’t bring down a production environment.

« Previous Page