Search code examples
axaptadynamics-ax-2012x++dynamics-ax-2009dynamics-ax-2012-r3

More detailed error from runbuf function in Dynamics AX


I try to build some kind of X++ script executor and therefore play around with the runbuf function. It works as long as the X++ code I pass in is valid, but when I pass invalid code then it just throws an error that it is not able to compile the code but no further details. For example when I try the following code

runbuf('void dynAdd(int lhs, int rhs) { return lhs + rhs; }');

it fails with the error

Unable to compile "void dynAdd(int lhs, int rhs) { return lhs + rhs; }".

Is there a way to get more information about the error?

Thanks in advance


Solution

  • You can use XppCompiler for that like so

    static void DynamicXppTest(Args _args)
    {
        str         dynamicXpp;
        int         result;
        XppCompiler xppCompiler;
        ;
    
        dynamicXpp = 'void dynAdd(int lhs, int rhs) { return lhs + rhs; }';
    
        // previous runbuf - style
        // 
        // result = runbuf(dynamicXpp, 3, 4);
        // info(strfmt("result = %1", result));
    
        xppCompiler = new XppCompiler();
        if (xppCompiler.compile(dynamicXpp))
        {
            result = xppCompiler.execute(3, 4);
            info(strfmt("result = %1", result));
        }
        else
        {
            error(xppCompiler.errorText());
        }
    }
    

    which will result in the below error in the infolog

    *** Error: 82, The operand is not compatible with the type of the function.