evalBuf Function

Evaluates the input string of X++ code, then returns the result as a string.


str evalBuf(str expressionString)

Parameter

Description

expressionString

The string to be evaluated.

The new string that contains the result of the expression.

For example, an input of 2 + 4 results in the string 6, not in the integer 6.

If an attacker can control input to the evalBuf function, it presents a security risk. Therefore, this function runs under Code Access Security. Calls to this function on the server require permission from the ExecutePermission class. Ensure that the user has development privileges by setting the security key to SysDevelopment on the control that calls this function.

The following example calls evalBuf to evaluate the supplied buffer.

static void JobEvalBufDemo(Args _args)
{
    ExecutePermission perm;
    str strCodeToExecute = "2 + 5";
    str strResult;
    ;
    perm = new ExecutePermission();
    if (perm != null)
    {
        // Grants permission to execute the EvalBuf function.
        // EvalBuf runs under code access security.
        perm.assert();
        // BP deviation documented.
        print "Next will execute the string of code.";
        pause; // Click Yes when asked to continue.

        strResult = EvalBuf(strCodeToExecute);
        // Close the code access permission scope.
        CodeAccessPermission::revertAssert();
    }
    print "strResult is: [", strResult ,"]";
    pause;
    //
    // Expected: "strResult is: [7]".
}

Community Additions

ADD
Show: