Tuesday, March 15, 2011

Any2str and the error message 'Internal error 25 in script'

The any2str function is a classic in Dynamics Ax.  It supports conversion from all sorts of data types into a string.  But as you might expect (and as is documented in the help files), only useful output is acquired when working with data types date, int and enum.

But the function cannot do magic.  Unfortunately.
If you take a look at the previous blog post, you might consider the any2str function a help in marshalling need.  (When doing a conversion between CLR primitives and X++ counterparts.)

Take a look at an example that I've copied from that earlier post:

static void DemoA(Args _args)
{
    ;
    info(strfmt('The current folder is %1',any2str(System.IO.Directory::GetCurrentDirectory())));
}

The code above will lead to the following error message:

Error executing code: Wrong type of argument for conversion function.

It gets followed by a more mysterious error message:

Internal error number 25 in script.

So this use of any2str is a definitely no go.

Here is an example: of a workaround:

static void DemoC(Args _args)
{
    str     myFolder=System.IO.Directory::GetCurrentDirectory();
    ;
    info(strfmt('The current folder is %1',myFolder));
}

So lesson learned: When using any2str, stick to the X++ data types as parameters.

If you don't like the workaround above, there is also a nice function in the CLRInterop class that will do the trick, namely getAnyTypeForObject. This function will convert the CLR object to an X++ anytype data type.

static void DemoC(Args _args)
{
    ;
    info(strfmt('The current folder is %1',ClrInterop::getAnyTypeForObject(System.IO.Directory::GetCurrentDirectory())));
}

This code example seems more correct.

No comments:

Post a Comment