AX 2012: evalBuf – Evaluate string expression in X++

evalBuf Function is a very strong API in X++. We can quite easily evaluate complex algebraic expressions given in string and the result is also given back in string. This API should be used along with CodeAccessPermission. The downside of using this API is that the code using this function is not supported in CIL.

This is how we can use this API

static void JobEvalBufDemo(Args _args)
{
    ExecutePermission perm;
    str strCodeToExecute = "2 + (5*10)";
    str strResult;
    ;

    perm = new ExecutePermission();

    if (perm != null)
    {
        perm.assert();				// Grants permission
        strResult = EvalBuf(strCodeToExecute);
        CodeAccessPermission::revertAssert();	// Revoke permission
    }

    info(strFmt("Result is: %1", strResult));
    // info: "Result is: 52"
}

2 thoughts on “AX 2012: evalBuf – Evaluate string expression in X++

Add yours

  1. Hi, thanks for this posting.

    Please, can you describe a scenario where the use of this API comes in handy?

    Perhaps you can tell whether or not this API is used within the product builder of Dynamics AX?

    /byteway

    1. I guess the PCConstraintEditor in the Product Configurator uses this API but am not sure. I came across this API to achieve a rare client requirement to have the ability to define algebraic expressions to calculate unit price of a product.

Leave a comment

Blog at WordPress.com.

Up ↑