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"
}
Advertisements

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

  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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s